Link to home
Start Free TrialLog in
Avatar of Jacob L
Jacob LFlag for United States of America

asked on

vb script to delete files in multiple subfolders by date

I have this script below which I copied from another solution from you experts.
However, I need some additions. I need to delete all files that are 10 days old in each folder and subfolder in an assigned directory.

Example:
in the root directory folder there are 10 subfolders. In each subfolder there are 3 more subfolders (folderA, folderB, and folderC). In those 3 folders (folderA, folderB, and folderC) are files in which I need to delete that are older than 10 days.

This code only deletes files in the 10 subfolders, but not folderA, folderB, and folderC.
Also note there are no files in the 10 subfolders, just more subfolders.

I hope this makes sense. I pasted the script below. Thanks in advance.

Option Explicit
 
Dim iDaysOld
Dim strPath
Dim objFSO
Dim objFolder
Dim objSubfolder
Dim objFile
Dim colSubfolders
Dim colFiles

iDaysOld = 10
strPath  = "C:\Users\jlepley\Desktop\test"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objSubfolder in colSubfolders
   Set colFiles = objSubfolder.Files
   For Each objFile in colFiles
       If objFile.DateLastModified < (Date() - iDaysOld) Then
           Wscript.Echo "Deleting: " & objFile.Path
           objFile.Delete
       End If
   Next

Next
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacob L

ASKER

Perfect. I new it had to be something simple like that but I never coded a vb script. Thanks a a lot.
No problem.  Thanks for the grade.

Rob.