deleting all installed files and folders without RmDir /r
I like to use /r when installing my application, but not when uninstalling since that will also delete files I did not install (as often recommended on this forum).
After looking at AdvUninstLog2.nsh and UninstallLog.nsh I decided to take a different approach to deleting only my installed files and not anything else. Maybe this has been posted before, but I didn't come across it.
Before compiling my NSIS script, I run the short Visual Basic program below to generate a nsh include file containing Delete and RmDir commands to remove everything from the user's computer.
In my NSIS script I do the installing like as follows. Here I am installing into Program Files and to the user's APPDATA folder.
SetOutPath "$INSTDIR"
File /a "Files\*.*"
SetOutPath "$INSTDIR\Network"
File /a /r "Files\Network\*.*"
SetShellVarContext all ;all means install for all users
SetOutPath "$APPDATA\Xlrotor"
File /a /r "Files\Xlrotor\*.*"
The good part is that to uninstall all that is needed is:
SetShellVarContext all
!include "DeleteInstalledFiles.nsh"
It doesn't win points for elegance, but it gets the the job done without much fuss. I run the visual basic program in Excel, but I it could be run from any Office application or in a VB6 or .Net program, too, I suppose. It only needs to be run once, or when the list of installed files changes.
I hope somebody finds it useful.
I wonder if a VB6 program could be run by the NSIS compiler when the script is compiled to update the include file before it is invoked later in the script. Can that be done?
Cheers,
Brian in Austin Texas
Sub do_it()
Close
Open ThisWorkbook.path & "\DeleteInstalledFiles.nsh" For Output As #1
'I do the folders sort of in reverse order so the RmDir commands will delete subfolders before parent folders
ListFolder ThisWorkbook.path & "\Files\XLRotor", """$APPDATA", True
ListFolder ThisWorkbook.path & "\Files\Network", """$INSTDIR", False
ListFolder ThisWorkbook.path & "\Files", """$INSTDIR", False
Close
End Sub
'If the folder structure is deeper than 2 levels, additional inner loops could be added.
Sub ListFolder(path$, prefix$, Recurse As Boolean)
Dim sFolderPath As String
sFolderPath = path
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim subsubfolder As Folder
Dim xFiles As Files
Dim oneFile As File
Dim i As Integer
Set FSfolder = FS.GetFolder(sFolderPath)
For Each oneFile In FSfolder.Files
Print #1, "Delete " & prefix & Mid(oneFile.path, InStr(oneFile.path, "\Files\") + 6) & """"
Next oneFile
If Recurse Then
For Each subfolder In FSfolder.SubFolders
DoEvents
i = i + 1
For Each oneFile In subfolder.Files
Print #1, "Delete " & prefix & Mid(oneFile.path, InStr(oneFile.path, "\Files\") + 6) & """"
Next
For Each subsubfolder In subfolder.SubFolders
DoEvents
i = i + 1
For Each oneFile In subsubfolder.Files
Print #1, "Delete " & prefix & Mid(oneFile.path, InStr(oneFile.path, "\Files\") + 6) & """"
Next
Print #1, "RmDir " & prefix & Mid(subsubfolder.path, InStr(subsubfolder.path, "\Files\") + 6) & """"
Next subsubfolder
Print #1, "RmDir " & prefix & Mid(subfolder.path, InStr(subfolder.path, "\Files\") + 6) & """"
Next subfolder
End If
Print #1, "RmDir " & prefix & Mid(FSfolder.path, InStr(FSfolder.path, "\Files") + 6) & """"
End Sub