Home > Scripting > VBScript – Delete files Older than X days

VBScript – Delete files Older than X days

This quick and dirty script will delete all files in a supplied directory older than X number of days. I have used this to clear still images from a surveillance system so it doesn’t fill the entire hard drive.

Warning this script is intended to delete data from a hard drive. Use at your own discretion I am not responsible for any data loss experienced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'Created by Dustin Berube 2/9/2009
'Last modified 2/9/2009

Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("path\to\folder")
Set fc = f.Files
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > < # of days> Then
f1.Delete
End If
Next
Set fso = Nothing
Set f = Nothing
Set fc = Nothing

Save this as a .vbs file and execute by running cscript filename.vbs

Categories: Scripting Tags:
  1. No comments yet.
  1. No trackbacks yet.