Uninstall all dirs and subdirs created by your installer

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

If we install to "C:\Program Files\My App\v#.#" then do a RMDir /r "C:\Program Files\My App\v#.#", only the folder "v#.#" will be deleted. The "C:\Program Files\My App" folder will remain. This function will ensure that any empty folders in the installation path will be deleted.

Usage

Push 10 #maximum amount of directories to remove
Push $INSTDIR #input string
 Call UninstallDirs

The Function

Function UninstallDirs
Exch $R0 ;input string
Exch
Exch $R1 ;maximum number of dirs to check for
Push $R2
Push $R3
Push $R4
Push $R5
   IfFileExists "$R0\*.*" 0 +2
   RMDir /rebootok "$R0"
 StrCpy $R5 0
top:
 StrCpy $R2 0
 StrLen $R4 $R0
loop:
 IntOp $R2 $R2 + 1
  StrCpy $R3 $R0 1 -$R2
 StrCmp $R2 $R4 exit
 StrCmp $R3 "\" 0 loop
  StrCpy $R0 $R0 -$R2
   IfFileExists "$R0\*.*" 0 +2
   RMDir /rebootok "$R0"
 IntOp $R5 $R5 + 1
 StrCmp $R5 $R1 exit top
exit:
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd

Created for tderouin after this topic on the NSIS Forums.

-Stu