[英]Is it possible to conditionally add a file/folder to NSIS installer
是否可以有条件地向 NSIS 安装程序添加文件/文件夹和安装选项? 我的想法是,如果文件夹Foo存在于给定位置,则应将其添加到安装程序中,并且安装Foo的选项也应添加到安装程序中。 但是如果文件夹Foo不存在,NSIS 脚本应该只创建安装程序,但保留Foo和从其中选择Foo的选项。
您可以尝试使用/NONFATAL包含文件。 如果存在,它将被编译器包含。 在运行时,您可以检查安装程序是否能够提取它。
File /NONFATAL "file.zip"
${If} ${FileExists} "$OUTDIR\file.zip"
...
${EndIf}
在 NSIS 2 File /NONFATAL /R "c:\\foo"
是最好的,你可以在没有外部工具的情况下做,当没有文件时,你需要一点技巧来隐藏该部分:
!include LogicLib.nsh
Page Components
Page InstFiles
Section "Main"
SetOutPath $InstDir
# File "C:\myfiles\myapp.exe"
SectionEnd
Section "Install Foo" SID_FOO
SetOutPath $InstDir
File /NONFATAL /r "C:\myfiles\foo\*.*"
SectionEnd
Function .onInit
SectionGetSize ${SID_FOO} $0
StrCmp $0 0 "" +3
SectionSetFlags ${SID_FOO} 0 ; Force all flags off including the checkmark
SectionSetText ${SID_FOO} "" ; Hide the section because its size is 0
FunctionEnd
如果这是不可接受的,您可以使用!system
并从 cmd.exe 获得一些帮助以检查是否存在某些内容:
!tempfile INCEXIST
!system 'if exist "C:\myfiles\foo\*.*" echo !define HAVE_FOO > "${INCEXIST}"'
!include "${INCEXIST}"
!delfile "${INCEXIST}"
!ifdef HAVE_FOO
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif
在 NSIS 3 !if
支持 /FileExists 开关:
!if /FileExists "C:\myfiles\foo\*.*"
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif
替换依赖于正在运行的服务并且在目标位置是否存在的文件的示例
IfFileExists "$SYSDIR\my_file.dll" exist notexist
exist:
ExecWait 'net stop desired_service'
SetOutPath $SYSDIR
SetOverwrite on
File "/oname=$SYSDIR\my_file.dll" "Path to my file\my_file.dll"
ExecWait 'net start desired_service'
notexist:
.....what you want to do if doesn't exists
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.