简体   繁体   中英

Is it possible to conditionally add a file/folder to NSIS installer

Is it possible to conditionally add a file/folder and installation option to a NSIS installer? My idea is that if the folder Foo exists at a given location it should be added to the installer and the option to install Foo should be added to the installer as well. But if the folder Foo does not exist, the NSIS script should just create the installer but leave Foo and the option to select Foo out of it.

You can try to include a file with /NONFATAL . If it exists, it will be included by the compiler. In runtime, you can check if installer was able to extract it.

File /NONFATAL "file.zip"
${If} ${FileExists}  "$OUTDIR\file.zip"
...
${EndIf}

In NSIS 2 File /NONFATAL /R "c:\\foo" is the best you can do without external tools and you need a little hack to hide the section when there are no files:

!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

If this is unacceptable you can use !system and get a little help from cmd.exe to check if something exists:

!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

In NSIS 3 !if supports a /FileExists switch:

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

Example to replace file what depends on running service and exists or not at targget location

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM