RATE THIS ANSWER
+1
Click to Vote:
1
0
A function to delete empty folders could be like this:
Function DeleteEmptyFolders(foldername)
For Each Folder In fso.GetFolder(foldername).SubFolders
DeleteEmptyFolders(Folder.Path)
If Folder.Files.Count = 0 and Folder.SubFolders.Count = 0 Then
fso.DeleteFolder(Folder.Path)
End If
Next
End Function
So, your modified scrip would be like this (I removed a function called DeleteOldfolder which was not being used)
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "c:\test1"
OlderThanDate = DateAdd("d", -30, Date) ' 30 days
DeleteOldFiles startFolder, OlderThanDate
DeleteEmptyFolders startFolder
Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Next
End Function
Function DeleteEmptyFolders(foldername)
For Each Folder In fso.GetFolder(foldername).SubFolders
DeleteEmptyFolders(Folder.Path)
If Folder.Files.Count = 0 and Folder.SubFolders.Count = 0 Then
fso.DeleteFolder(Folder.Path)
End If
Next
End Function
Don't forget to test the modified script before using it in a production environment.
Last Answered:
Nov 23 2009 7:41 PM GMT by Carlosdl 
32725 pts.