Talk:Recursively remove empty parent directories

From NSIS Wiki
Jump to navigationJump to search

This script will not work for empty parent directories directly under root (NSIS 2.46). Say your app is under C:\test\myapp. In your uninstall section you call:

RMDir "$INSTDIR"
${RMDirUP} "$INSTDIR"

This does remove myapps and test (provided they are empty), but then gets into a loop when trying to remove C:\test\myapp\..\..

RMDir on the root does not give an error and the script recursively continues with

RMDir C:\test\myapp\..\..\..<br\> RMDir C:\test\myapp\..\..\..\..<br\> RMDir C:\test\myapp\..\..\..\..\..<br\> etc.

After some time the uninstaller exits and everything in your uninstall section after the RMDirUP call is not executed.

I'm not sure if this is a flaw in the script or a bug in the RMDir instruction. My proposal would be to change the function as follows:

!include "FileFunc.nsh"
 
;--
;--
 
Function un.RMDirUP
  !define RMDirUP '!insertmacro RMDirUPCall'
  !macro RMDirUPCall _PATH
    push '${_PATH}'
    Call un.RMDirUP
  !macroend
  ; $0 - current folder
  ClearErrors
  Exch $0
  ${GetParent} "$0" $R0
  ${GetRoot} "$0" $R1
  StrCmp $R0 $R1 Skip 0
  RMDir "$R0"
  IfErrors Skip
    ${RMDirUP} "$R0"
  Skip:
  Pop $0
FunctionEnd

Fkaatje 12:26, 11 August 2011 (UTC)