NSIS 檔案判斷存在與否/判斷檔案是否存在(IfFileExists Changes Section Flags)
NSIS 檔案判斷存在與否/判斷檔案是否存在(IfFileExists Changes Section Flags)
資料來源:http://nsis.sourceforge.net/IfFileExists_Changes_Section_Flags
!include Sections.nsh
Name TestSelectSection
OutFile "TestSelectSection.exe"
Page components
Page instfiles
ShowInstDetails show
; This file will exist on most computers
Section /o "autoexec.bat detected" autoexec_detected
MessageBox MB_OK autoexec
SectionEnd
Section /o "Boot.ini detected" boot_detected
MessageBox MB_OK boot
SectionEnd
Section /o "non-existing file detected" missing_detected
MessageBox MB_OK missing
SectionEnd
Function .onInit
IfFileExists C:\autoexec.bat AutoexecExists PastAutoexecCheck
AutoexecExists:
; This is what is done by sections.nsh SelectSection macro
SectionGetFlags "${autoexec_detected}" $0
IntOp $0 $0 | ${SF_SELECTED}
SectionSetFlags "${autoexec_detected}" $0
PastAutoexecCheck:
IfFileExists C:\boot.ini BootExists PastBootCheck
BootExists:
; Use the macro from sections.nsh
!insertmacro SelectSection ${boot_detected}
PastBootCheck:
IfFileExists C:\xyz_missing.xyz MissingExists PastMissingCheck
MissingExists:
!insertmacro SelectSection ${missing_detected}
PastMissingCheck:
FunctionEnd
——————————————————————-
如果計劃執行要麼塊或另一個塊,不要忘記跳過第二塊。
這樣一個簡單的例子是:
IfFileExists "$INSTDIR\file.txt" file_found file_not_found file_found: StrCpy $0 "the file was found" goto end_of_test ;<== important for not continuing on the else branch file_not_found: StrCpy $0 "the file was NOT found" end_of_test: