Avatar of Jeffrey Renfroe
Jeffrey Renfroe
Flag for United States of America

asked on 

Delete files and folders older than 30 days using a vbs

Hello. I located a script here that deleted files that were 1 day old. I modified it to delete files 30 days old from the user's temp.

The problem I am having is getting the script to delete folders and files within folders that are over 30 days old. Is it possible to get a vbs to perform this function?

Thank you for any assistance.
Dim iDaysOld
Dim objFSO
Dim objShell
Dim strEnv
Dim sDirectoryPath
Dim objFolder
Dim objFileCollection
Dim objFile
 
iDaysOld = 30
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strEnv = objShell.ExpandEnvironmentStrings("%temp%")
sDirectoryPath = strEnv
Set objFolder = objFSO.GetFolder(sDirectoryPath)
Set objFileCollection = objFolder.Files
For Each objFile in objFileCollection
If objFile.DateLastModified < (Date() - iDaysOld) Then
objFile.Delete(True)
End If
Next
 
Set objFSO = Nothing
Set objFolder = Nothing
Set objFileCollection = Nothing
Set objFile = Nothing

Open in new window

VB Script

Avatar of undefined
Last Comment
Jeffrey Renfroe

8/22/2022 - Mon