Search code examples
nsis

How to make NSIS RMDir operate on subdirectories?


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

Solution

  • 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