Question Locked

Help with VB Script

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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 it to delete 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. Note, I do not want the test folder to be deleted only the empty subfolders.

Thanks in advance for your help.

*****************VB Script**************************
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
*****************************************************************

6 replies

by john.a.wills

The function DeleteOldFolder (which should really be a procedure) seems not to get invoked.

by jutpro

I would appreciate any help you can offer in how I can correct this procedure.
I know absolutely nothing about VB Scripting.

The only problem with this script is it does not delete the empty subfolders.

Thanks

by brenton keegan

I'd really spend some time to learn vbscript if you're going to try to code a script that deletes files.

Look up "Object Oriented programming", that will help you to really understand the structure.

by jutpro

Thanks to everyone. I was able to get the script to work fine.

This discussion is closed to new replies.