在NSIS安装程序脚本中,我有:
RMDir "$INSTDIR"
现在,如果用户将安装目录设置为C:\Program Files\Product
,它可以正常工作,但是如果它们安装到更深层的东西,例如C:\Program Files\Company\Product
,则RMDir会删除“Product”但不会“公司”。如何将每个空目录删除到根目录(不使用/ r)...例如如果为空,删除产品,如果为空则删除公司,如果为空则删除程序文件,依此类推?
编辑:我最终使用的功能:
# Delete empty directories recursively
var deleteDir
var dirLength
Function un.PathDeleteEmptyDirRecurse
ClearErrors
loop:
Sleep 50 ; Without a small delay here, the directory sometimes won't get removed
RMDir "$deleteDir" ; Remove the directory
IfErrors end
strlen $dirLength $deleteDir ; Store the length of the path
intcmp $dirLength 3 end end ; If the length of the path is <= 3 (e.g. C:\), we're at the root drive
GetFullPathName $deleteDir "$deleteDir\.." ; <path>\.. results in the parent directory of <path>
IfErrors end loop
end:
FunctionEnd
答案 0 :(得分:3)
我假设你想在卸载程序中使用它而不是安装程序:
Function un.PathDeleteEmptyDirRecurse
exch $0
push $1
ClearErrors
loop:
RMDir $0
IfErrors end
strlen $1 $0
intcmp $1 3 end end ;root of drive?
GetFullPathName $0 "$0\.."
IfErrors end loop
end:
pop $1
pop $0
FunctionEnd
...
push $instdir
call un.PathDeleteEmptyDirRecurse
答案 1 :(得分:0)
这是我使用的;
Function un.RMDirUP
!define RMDirUP '!insertmacro RMDirUPCall'
!macro RMDirUPCall _PATH
push '${_PATH}'
Call un.RMDirUP
!macroend
; $0 - current folder
ClearErrors
Exch $0
RMDir "$0\.."
IfErrors Skip
${RMDirUP} "$0\.."
Skip:
Pop $0
FunctionEnd
这是让你在卸载时调用它
${RMDirUP} "$INSTDIR"
答案 2 :(得分:-2)
我最终只使用这两行来删除安装目录中的所有内容和目录本身(如果为空):
RMDir /r "$INSTDIR"
RMDir "$INSTDIR\..\."