NSIS,在安装过程中检测目录是否存在

use*_*718 3 installation nsis

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

谢谢您的任何建议。

And*_*ers 5

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

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
Run Code Online (Sandbox Code Playgroud)

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

!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
Run Code Online (Sandbox Code Playgroud)