NSIS,在安装期间检测目录是否存在

时间:2015-04-10 12:04:25

标签: installation nsis

我有安装程序,它支持选择安装目录。我想检测给定的文件夹是否存在以及是否为空。如果它不为空,则显示警告消息框,然后删除其所有内容并将程序安装到该文件夹​​中。唯一的问题是进入正确的代码部分,我可以在安装过程中获得用户给出的安装文件夹,我可以处理其余部分。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:4)

通常您只需检查目录是否存在:

Outfile "$%Temp%\Test.exe"
RequestExecutionLevel user
InstallDir "$Documents\Test"

!include LogicLib.nsh

Page Directory "" "" DirLeave
Page InstFiles

Function DirLeave
${If} ${FileExists} "$InstDir\*"
    MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
    Abort
yep:
    RMDir /r "$InstDir"
${EndIf}
FunctionEnd

Section
SetOutPath $InstDir
File myfile.ext
SectionEnd

如果目录存在但是为空,这也将显示消息。要解决这个问题,您需要进行一些自定义检测:

!macro _IsNonEmptyDirectory _a _b _t _f
!insertmacro _LOGICLIB_TEMP
!insertmacro _IncreaseCounter
Push $0
FindFirst $0 $_LOGICLIB_TEMP "${_b}\*"
_IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}:
    StrCmp "" $_LOGICLIB_TEMP _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
    StrCmp "." $_LOGICLIB_TEMP +2
    StrCmp ".." $_LOGICLIB_TEMP 0 _IsNonEmptyDirectory_done${LOGICLIB_COUNTER}
    FindNext $0 $_LOGICLIB_TEMP
    Goto _IsNonEmptyDirectory_loop${LOGICLIB_COUNTER}
_IsNonEmptyDirectory_done${LOGICLIB_COUNTER}:
FindClose $0
Pop $0
!insertmacro _!= "" $_LOGICLIB_TEMP `${_t}` `${_f}`
!macroend
!define IsNonEmptyDirectory `"" IsNonEmptyDirectory`

Function DirLeave
${If} ${IsNonEmptyDirectory} "$InstDir"
    MessageBox MB_YESNO `"$InstDir" already exists, delete it's content and continue installing?` IDYES yep
    Abort
yep:
    RMDir /r "$InstDir"
${EndIf}
FunctionEnd