Help with VB Script - TechRepublic
Question
November 23, 2009 at 08:38 AM
jutpro

Help with VB Script

by jutpro . Updated 16 years, 7 months ago

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

This discussion is locked

All Comments