The following article uses options that are available starting with the Professional edition and project type.
The first step in removing a file or a folder is finding its path. For this, you can use a "New Search" or a "New File Search" in the Search page.
Find the path of a folder
This can be done by following these steps:
- Go to the "Search" page and create a "New Search"
- While it is selected in the tree control, press F2 and rename it to something meaningful (for example MY_FOLDER)
- Right-click the search and use the "Add Location" menu to add a Folder location
- In the Directory Properties page, configure the folder you want to find
The property used by the search ("MY_FOLDER") will contain the path to the folder (if it has been found).
Removing a folder
Under normal circumstances, a folder installed by the package will be uninstalled automatically. However, if it contains resources which were not created by the installation package, the folder will not be removed.
If you know the files which remain, you can use File Removal Operations to remove all the files in the folder. However, if you don't know what the folder will contain, you can use a custom action:
Function removeFolders() custActData = Session.Property("CustomActionData") foldersArray = Split(custActData, "|", -1) Set objFS = CreateObject("Scripting.FileSystemObject") For Each folder In foldersArray smallPath = RemoveTrailingBackslash(folder) If (objFS.FolderExists(smallPath)) Then objFS.DeleteFolder smallPath, true End if Next End Function Function RemoveTrailingBackslash(folderPath) If Right(folderPath, 1) = "\" Then RemoveTrailingBackslash = Left(folderPath, Len(folderPath) - 1) Else RemoveTrailingBackslash = folderPath End If End Function
This code can be pasted into a new text file with a .VBS extension which can be added as an Attached custom action. You can schedule this custom action anywhere between InstallInitialize and InstallFinalize standard actions, but keep in mind the custom action must be set as deferred in order to properly work under Windows Vista and above.
The folders to be deleted will be specified in the Action Data field, separated by the pipe ("|") character, as showed below. Configure this custom action as follows:
- Function Name: removeFolders
- Execution Time: When the system is modified (deferred)
- Execution Condition: it depends on when exactly you want the custom action to run. If you want to delete the folders when the package is installed, use (NOT Installed) as the Execution Condition; if the folders should be deleted at uninstall, use (REMOVE="ALL").
- Action Data: A "|"-separated list of folders to be deleted. You can include Formatted values (including folders found by a Search) as well as hard-coded folder paths, for instance:
Find the folder which contains a file
In order to find the path of the folder which contains your file, you first need to find the file. This can be done by following these steps:
- Go to the "Search" page and create a "New File Search"
- Rename it to something meaningful (for example MY_FILE)
- Configure the "File to Search" operation's properties to search for your file
- Right-click the search and use the "Add Location" menu to add a Folder location in which to search the file
The property used by the search ("MY_FILE") will contain the path to the file (if it has been found). In order to obtain the path of the folder which contains this file, you can use a custom action:
Function GetParentFolder() filePath = Session.Property("MY_FILE") Set objFS = CreateObject("Scripting.FileSystemObject") If (objFS.FileExists(filePath)) Then Session.Property("PARENT_FOLDER") = AppendTrailingBackslash(objFS.GetParentFolderName(filePath)) End If End Function Function AppendTrailingBackslash(folderPath) If Right(folderPath, 1) <> "\" Then AppendTrailingBackslash = folderPath & "\" Else AppendTrailingBackslash = folderPath End If End Function
The custom actions which affect the result of a search can be scheduled under the "Install Execution Stage" -> "Searches" action group.
The Function Name for the above custom action will be set to GetParentFolder. The custom action will be set to execute Immediately.
Once you have the path of the folder which contains your file you have two options for removing it:
- You use a File Removal operation to remove the file
- You use the custom action described above which removes the entire folder
If you use a file removal operation you can follow these steps:
- Go to the "Files and Folders" page and create a property-based folder by using the folder which contains the file (the "PARENT_FOLDER" property)
- In this folder, create a File Removal operation which removes your file
- Assign the file removal to a component which will be installed by your package