145 pts.
Q:
Help with VB Script to delete old files and subfolders
Hello, Can someone give me some help on the below VB script. Unfortunately I am not a VB programmer. I pasted together different portions of scripts and the below is the script I have. This script works in deleting all files that are 30 days old from the main folder, 'test",  and all subfolders. However, I cannot get  the empty subfolders deleted. Note, I do not want the test folder to be deleted only the empty subfolders.   Can someone be so kind as to look at the below script and let me know what needs to be modified to get it to delete the empty subfolders.  Thanks in advance for your help. ******************************************* Dim fso, startFolder, OlderThanDate Set fso = CreateObject("Scripting.FileSystemObject" startFolder = "c:test" OlderThanDate = DateAdd("d", -30, Date) ' 30 days DeleteOldFiles startFolder, OlderThanDate 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 DeleteOldfolder(foldername, BeforeDate) Set folderlist = fso.GetFolder(foldername) Set folderCollection = Folderlist.SubFolders For Each Folder In folderCollection If folder.DateLastModified <  BeforeDate Then fso.DeleteFolder(folder.Path) End If Next End Function *****************************************************************[o:p][/o:p]

ASKED: Nov 23 2009  4:29 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
32725 pts.
A:
 RATE THIS ANSWER
+1
Click to Vote:
  •   1
  •  0
  • Bookmark and Share
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.
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _



_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Jutpro   145 pts.  |   Nov 23 2009  8:05PM GMT

Thnk you so very much Carlosdl. I am most grateful for your help.

I implemented your changes and tested the script and it is working as it should. The empty subfolders are now being deleted.
Have a great week.