Skip to content

Instantly share code, notes, and snippets.

@Grimthorr
Last active July 25, 2019 14:17
Show Gist options
  • Save Grimthorr/2dfdeb2e238b1b331264 to your computer and use it in GitHub Desktop.
Save Grimthorr/2dfdeb2e238b1b331264 to your computer and use it in GitHub Desktop.
A Visual Basic script to delete files from the specified folder that are older than the specified number of days.
On Error Resume Next
'Dim some stuffs
Dim objFso, objFolder, objFile, strFolderPath, intDaysOlderThan
Set objFso = CreateObject("Scripting.FileSystemObject")
'Change these variables
strFolderPath = "C:\Temp"
intDaysOlderThan = 28
'Do the magic
Call DeleteFiles(strFolderPath, intDaysOlderThan)
'Procedures
Sub DeleteFiles(path, days)
Set objFolder = objFso.GetFolder(path)
For Each objFile In objFolder.Files
If objFile.DateLastModified < (Now() - days) Then
objFile.Delete(True)
End If
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment