【问题标题】:NSIS, detect if directory exists during installationNSIS,在安装过程中检测目录是否存在
【发布时间】:2015-04-10 12:04:25
【问题描述】:

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

感谢您的任何建议。

【问题讨论】:

    标签: installation nsis


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多