Skip to content

Instantly share code, notes, and snippets.

@mreschke
Last active February 2, 2016 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreschke/34359997d795b5564498 to your computer and use it in GitHub Desktop.
Save mreschke/34359997d795b5564498 to your computer and use it in GitHub Desktop.
Delete old Windows files based on age and recursion depth
' A windows vbscript to delete old files. Simply make a .bat file and thow it in Windows scheduled tasks
' Example, same file in C:\batch\DeleteOldFiles.vbs and make C:\batch\CleanupThisServer.bat:
'
' cd C:\batch
' cscript DeleteOldFiles.vbs "D:\SMTPExpress\EasyMail SMTP Express\logfiles" 90
' cscript DeleteOldFiles.vbs "D:\DynaMailPDF\" 720 txt 2 (this one uses max depth =2)
'--------------------------------------------------------------------------------------
' Objective: To delete old files from a given folder and all subfolders below
' Created by: MAK
' Created Date: June 21, 2005
' Usage: cscript deloldfiles.vbs c:\dba\log 3 (delete files older than 3 days)
'--------------------------------------------------------------------------------------
' mReschke 2012-04-05 added option to delete only by extension and limit recursion to maxdepth
' Usage: cscript deloldfiles.vbs c:\dba\log 3 csv (delete only .csv files)
' Usage: cscript deloldfiles.vbs c:\dba\log 3 csv 1 (delete only .csv files in parent dir (no recursion))
' Usage: cscript deloldfiles.vbs c:\dba\log 3 csv 2 (delete only .csv files in parent dir and one subdir (2 maxdepth recursion))
'--------------------------------------------------------------------------------------
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)
'mReschke Extension and MaxDepth
for each item in objArgs:x = x + 1:next
ext="" : maxdepth = 0
if x >= 3 then ext=lcase(objArgs(2))
if x >= 4 then maxdepth=cint(objArgs(3))
'MsgBox("Ext: " & ext & ", maxdepth: " & maxdepth)
set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders, ext, maxdepth, 0
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"
sub recurse( byref folders, byval ext, byval maxdepth, byval recursion_count )
set subfolders = folders.subfolders
set files = folders.files
recursion_count = recursion_count + 1
wscript.echo ""
wscript.echo "Deleting Files under the Folder:" & folders.path
'MsgBox("Recursing Now: " & recursion_count)
wscript.echo "__________________________________________________________________________"
for each file in files
if ext="" or right(lcase(file.name),len(ext))=ext then
if file.datelastmodified < newdate then
wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
on error resume next
'Delete File
file.delete
end if
end if
next
'Recurse into directory if enabled
'mReschke Disable Recursion
for each folder in subfolders
'MsgBox("Just Before: " & recursion_count & ", max: " & maxdepth)
if recursion_count <= maxdepth-1 or maxdepth = 0 then
'MsgBox("Running recursion because " & recursion_count & " <= " & maxdepth)
recurse folder, ext, maxdepth, recursion_count
end if
next
set subfolders = nothing
set files = nothing
recursion_count = recursion_count - 1
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment