Check if a Folder is Empty (contains no files and no subfolders)
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.
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