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