如何让 NSIS RMDir 对子目录进行操作?

发布于 2024-09-10 21:26:18 字数 953 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-09-17 21:26:18

我假设您希望在卸载程序中而不是在安装程序中使用此命令:

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

I assume you want this in the uninstaller and not the installer:

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
∝单色的世界 2024-09-17 21:26:18

这是我用的;

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"

Here's what I use;

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

This let's you call it in the uninstall with
${RMDirUP} "$INSTDIR"

天气好吗我好吗 2024-09-17 21:26:18

我最终仅使用这两行来删除安装目录和目录本身(如果为空)中的所有内容

RMDir /r "$INSTDIR"
RMDir "$INSTDIR\..\."

I finally ended up using just these two lines to delete everything in the installation directory and the directory itself (if empty):

RMDir /r "$INSTDIR"
RMDir "$INSTDIR\..\."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文