是否可以有条件地将文件/文件夹添加到NSIS安装程序

是否可以有条件地将文件/文件夹添加到NSIS安装程序,nsis,Nsis,是否可以有条件地向NSIS安装程序添加文件/文件夹和安装选项? 我的想法是,如果文件夹Foo存在于给定的位置,那么应该将其添加到安装程序中,并且安装Foo的选项也应该添加到安装程序中。但是,如果文件夹Foo不存在,NSIS脚本应该只创建安装程序,但保留Foo和从中选择Foo的选项。您可以尝试将文件包含在其中。如果它存在,编译器将包含它。在运行时,您可以检查安装程序是否能够提取它 File /NONFATAL "file.zip" ${If} ${FileExists} "$OUTDIR\fil

是否可以有条件地向NSIS安装程序添加文件/文件夹和安装选项?
我的想法是,如果文件夹Foo存在于给定的位置,那么应该将其添加到安装程序中,并且安装Foo的选项也应该添加到安装程序中。但是,如果文件夹Foo不存在,NSIS脚本应该只创建安装程序,但保留Foo和从中选择Foo的选项。

您可以尝试将文件包含在其中。如果它存在,编译器将包含它。在运行时,您可以检查安装程序是否能够提取它

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
如果这是不可接受的,您可以使用
!系统
,并从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
中!如果
支持/FileExists开关:

!if /FileExists "C:\myfiles\foo\*.*"
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif

替换文件的示例取决于正在运行的服务,该文件是否存在于targget位置

    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