仅卸载已安装的文件 NSIS

Uninstall only installed files NSIS

我想找到一种只卸载已安装文件的方法。 我试过 http://nsis.sourceforge.net/Uninstall_only_installed_files 但我有大约 10.000 个文件嵌套在许多目录中,因此在 nsis 脚本中单独指定每个文件不是解决方案。

您可以使用!system命令在编译时执行外部工具。此外部 tool/script 应生成包含安装和卸载说明的包含文件。

您也可以即时使用 NSIS 生成指令,但是当您将所有指令放在一个脚本中时会有点混乱:

; Generate some files for this example:
!system 'mkdir "$%temp%\testdir\subdir\sub2\sub3"'
!appendfile "$%temp%\testdir\file1.txt" ""
!appendfile "$%temp%\testdir\subdir\file2.txt" ""
!appendfile "$%temp%\testdir\subdir\sub2\file3.txt" ""
!appendfile "$%temp%\testdir\subdir\sub2\sub3\file4.txt" ""



!macro GENFILELIST_Enum SourceDir InstDir
Push "${SourceDir}"
Push "${InstDir}"
Call GENFILELIST_Enum
!macroend

### Start editing here ###

!macro GeneratePages
OutFile "test.exe"
Name "Test"
RequestExecutionLevel admin
InstallDir "$ProgramFiles\MyTestApp"

Page Directory
Page InstFiles
UninstPage UninstConfirm
UninstPage InstFiles
!macroend

!macro GENFILELIST
!insertmacro GENFILELIST_Enum "$%temp%\testdir" "" ; Files to install
!macroend

!macro GenerateInstallSections FileInstructions
Section
SetOutPath $InstDir
!include "${FileInstructions}"
WriteUninstaller "$InstDir\Uninst.exe"
SectionEnd
!macroend

!macro GenerateUninstallSections FileInstructions
Section Uninstall
SetOutPath $InstDir
!include "${FileInstructions}"
SetOutPath $Temp
Delete "$InstDir\Uninst.exe"
RmDir $InstDir
SectionEnd
!macroend

### Stop editing here ###


!ifndef GENFILELIST_IN
!tempfile GENFILELISTAPP
!tempfile GENFILELIST_IN
!tempfile GENFILELIST_UN
!appendfile "${GENFILELIST_IN}" '!define GENFILELIST_UN "${GENFILELIST_UN}"$\n'
!appendfile "${GENFILELIST_IN}" 'OutFile "${GENFILELISTAPP}"$\n'
!system '"${NSISDIR}/makensis" "/DGENFILELIST_IN=${GENFILELIST_IN}" "${__FILE__}"' = 0
!system '"${GENFILELISTAPP}" /S' = 0
!delfile "${GENFILELISTAPP}"
!insertmacro GeneratePages
!insertmacro GenerateInstallSections "${GENFILELIST_IN}"
!insertmacro GenerateUninstallSections "${GENFILELIST_UN}"
!delfile "${GENFILELIST_IN}"
!delfile "${GENFILELIST_UN}"
!else
!include LogicLib.nsh
!include "${GENFILELIST_IN}"
!delfile "${GENFILELIST_IN}"
!macro GENFILELIST_Append file string tmpvar
FileOpen ${tmpvar} "${file}" a
FileSeek ${tmpvar} 0 END
FileWrite ${tmpvar} '${string}'
FileClose ${tmpvar}
!macroend
!define DOLLAR "$$"
Function GENFILELIST_Enum
System::Store S
Pop  ; Relative path
Pop  ; Base path
${IfThen}  == "" ${|} StrCpy  "${DOLLAR}OutDir" ${|}
FindFirst [=10=]  "\*"
loop:
StrCmp  "" stop
    StrCmp  "." next
    StrCmp  ".." next
    StrCpy  
    ${If} ${FileExists} "$1\*"
        !insertmacro GENFILELIST_Append "${GENFILELIST_IN}" 'SetOutPath "${DOLLAR}OutDir$1"$\n' 
        Push $1
        Push $1
        Call GENFILELIST_Enum
    ${Else}
        !insertmacro GENFILELIST_Append "${GENFILELIST_IN}" 'File "$1"$\n' 
        !insertmacro GENFILELIST_Append "${GENFILELIST_UN}" 'Delete "$1"$\n' 
    ${EndIf}
    next:
    FindNext [=10=] 
    Goto loop
stop:
FindClose [=10=]
!insertmacro GENFILELIST_Append "${GENFILELIST_UN}" 'RmDir ""$\n' 
System::Store L
FunctionEnd
RequestExecutionLevel user
Section
!insertmacro GENFILELIST
SectionEnd
!endif