Home » MSI (Windows Installer) » Useful Scripts for Custom Actions » Check if a Folder is Empty (contains no files and no subfolders)

Check if a Folder is Empty (contains no files and no subfolders)

Posted on by

Description:

Useful when we want to delete folders which have been left behind after uninstall, but we first need to check that the folder is actually empty!! An example may be a folder named as the vendor (E.g, ‘Adobe’), where other products could also be installed into the same folder (E.g, Acrobat Reader, Acrobat Standard, Creative Suite etc). This script will check if a folder is empty (contains no files and no subfolders) and delete it if so.

Source:

www.winfrastructure.net

Script:

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim folderToDelete : folderToDelete = "C:\FolderName\"

If objFSO.FolderExists(folderToDelete) Then

	Dim objFolder : Set objFolder = objFSO.GetFolder(folderToDelete)

	If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then
		objFolder.Delete(true)
	End If

End If

Set objFSO = Nothing

 

Comments have now been disabled. If you have a question to ask about this post please ask the community!