Jan 9 2009 3:59PM GMT
Posted by: Spencer Kuziw
VBScript to Delete Files in a Directory Older Than X Days
Posted by: Spencer Kuziw
I recently needed to whip up a quick script to delete all files in a directory that were older than X number of days. Hopefully it is something that you can use, too. Copy the code and paste it into a text file named something like “cleanup-folder.vbs” and set the variables for strDir (the absolute path to the directory) and strDays (files older than this number of days will be deleted).
CODE:
-
‘ ################################################################
-
‘ # cleanup-folder.vbs
-
‘ #
-
‘ # Removes all files older than 1 week
-
‘ # Authored by Spencer Kuziw (s.kuziw-at-epic.ca)
-
‘ # Based on code by YellowShoe
-
‘ # Version 1.0 - Sept 23 2008
-
‘ ################################################################
-
-
Dim fso, f, f1, fc, strComments, strScanDir
-
-
‘ user variables
-
‘ —————————————————————-
-
-
strDir = “FULL\PATH\TO\FOLDER\TO\BE\CLEANED”
-
strDays = 7
-
-
‘ DO NOT EDIT BELOW THIS LINE
-
‘ (unless you know what you are doing)
-
‘——————————————————————
-
Set fso = CreateObject("Scripting.FileSystemObject")
-
Set f = fso.GetFolder(strDir)
-
Set fc = f.Files
-
For Each f1 in fc
-
If DateDiff("d", f1.DateCreated, Date)> strDays Then
-
‘strComments = strComments & f1.name & ” “ & f1.DateCreated & vbCrLf
-
fso.DeleteFile(f1)
-
End If
-
Next
-
-
‘wscript.echo strComments
-
WScript.Quit
-
‘ eof



You must be logged-in to post a comment. Log-in/Register