如何让 NSIS RMDir 对子目录进行操作?
在 NSIS 安装程序脚本中,我有:
RMDir "$INSTDIR"
现在,如果用户将安装目录设置为 C:\Program Files\Product
,它可以正常工作,但是如果他们安装到更深的目录,例如 例如,C:\Program Files\Company\Product
,RMDir 会删除“Product”,但不会删除“Company”。我怎样才能让它删除每个空目录到根目录(不使用/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
In an NSIS installer script I have:
RMDir "$INSTDIR"
Now, if the user sets the installation directory to C:\Program Files\Product
, it works fine, however if they install to something deeper, such as C:\Program Files\Company\Product
for example, RMDir gets rid of "Product" but not "Company". How can I make it delete each empty directory down to the root (WITHOUT using /r)... e.g. delete Product if empty, delete Company if empty, delete Program Files if empty, and so on?
EDIT: The function I ended up using:
# 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您希望在卸载程序中而不是在安装程序中使用此命令:
I assume you want this in the uninstaller and not the installer:
这是我用的;
这让你可以在卸载中调用它
${RMDirUP} "$INSTDIR"
Here's what I use;
This let's you call it in the uninstall with
${RMDirUP} "$INSTDIR"
我最终仅使用这两行来删除安装目录和目录本身(如果为空)中的所有内容:
I finally ended up using just these two lines to delete everything in the installation directory and the directory itself (if empty):