Help with VB Script to delete old files and subfolders

480 pts.
Tags:
Development
Microsoft Windows
Programming
VBScript
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]

Answer Wiki

Thanks. We'll let you know when a new response is added.

A function to delete empty folders could be like this:

<pre>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</pre>

So, your modified scrip would be like this (I removed a function called DeleteOldfolder which was not being used)

<pre>Dim fso, startFolder, OlderThanDate
Set fso = CreateObject(“Scripting.FileSystemObject”)
startFolder = “c:\test1″
OlderThanDate = DateAdd(“d”, -30, Date) ‘ 30 days
DeleteOldFiles startFolder, OlderThanDate
<b>DeleteEmptyFolders startFolder</b>

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

<b>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</b></pre>

Don’t forget to test the modified script before using it in a production environment.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • jutpro
    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.
    480 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following