Post Content


This script will recursively delete all files in every subfolder older than X days. Will not delete any folders, just files.

Usage[-][--][++]

I create a batch file and call the vbs script for any directory I need purged.

rem ##### Use this script to clean up any old files automatically.
rem ##### This .bat will be called weekly from a scheduled task, and will
rem ##### call the d:\production\batch\generic\DeleteOldFiles.vbs with directory/day parameters
rem ##### the commands will not run simultaneously (unless they begin with start)
rem ##### mReschke 2009-04-03
d:
cd \Production\batch\generic\

cscript DeleteOldFiles.vbs "D:\SMTPExpress\EasyMail SMTP Express\failed" 180
cscript DeleteOldFiles.vbs "C:\WINDOWS\system32\LogFiles" 30

Script[-][--][++]

' 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
'      : It deletes files older than 3 days
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)
 
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 
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate 
wscript.echo "________________________________________________"
 
sub recurse( byref folders)
  set subfolders = folders.subfolders
  set files = folders.files
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
    file.delete
    end if
    
  next  
 
  for each folder in subfolders
    recurse folder
  next  
 
  set subfolders = nothing
  set files = nothing
 
end sub