How to remove a file or a folder during installation

ImportantThe 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:

  1. Go to the "Search" page and create a "New Search"
  2. While it is selected in the tree control, press F2 and rename it to something meaningful (for example MY_FOLDER)
  3. Right-click the search and use the "Add Location" menu to add a Folder location
  4. 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:
[ProgramFilesFolder]folder1|C:\my_folder|[MY_FOLDER]

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:

  1. Go to the "Search" page and create a "New File Search"
  2. Rename it to something meaningful (for example MY_FILE)
  3. Configure the "File to Search" operation's properties to search for your file
  4. 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

NoteThe custom actions which affect the result of a search can be scheduled under the "Install Execution Stage" -> "Searches" action group.

ImportantThe Function Name for the above custom action will be set to GetParentFolder. The custom action will be set to execute Immediately.

Remove a file

Once you have the path of the folder which contains your file you have two options for removing it:

  1. You use a File Removal operation to remove the file
  2. You use the custom action described above which removes the entire folder

If you use a file removal operation you can follow these steps:

  1. 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)
  2. In this folder, create a File Removal operation which removes your file
  3. Assign the file removal to a component which will be installed by your package