Monday, October 4, 2010

Delete Files Older Than X Days Recursely

  1. Save as vbs - deletefiles.vbs
  2. to use pass in number folder and number of days
For example deletefiles.vbs c:\temp 60

' Pass the arguments
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
path = Wscript.Arguments.Item(0)

' folder to start search in... You can just use static
'path = "C:\Temp\test"


' delete files older than 2 days...
mDays = Wscript.Arguments.Item(1)

killdate = date() - mDays

arFiles = Array()
set fso = createobject("scripting.filesystemobject")


' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true


nDeleted = 0
for n = 0 to ubound(arFiles)
  '=================================================
  ' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
  '=================================================
  on error resume next 'in case of 'in use' files...
  arFiles(n).delete true
  if err.number <> 0 then
    wscript.echo "Unable to delete: " & arFiles(n).path
  else
    nDeleted = nDeleted + 1
    'start 20100630 schneiderbi
    wscript.echo arFiles(n).path
    'end 20100630 schneiderbi
  end if
  on error goto 0
next


'msgbox nDeleted & " of " & ubound(arFiles)+1 _
'  & " eligible files were deleted"


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
  on error resume next
  'select files to delete and add to array...
  '
  set folder = fso.getfolder(sPath)
  set files = folder.files


  for each file in files
    ' uses error trapping around access to the
    ' Date property just to be safe
    '
    dtlastmodified = null
    on error resume Next
    dtlastmodified = file.datelastmodified
    on error goto 0
    if not isnull(dtlastmodified) Then
      if dtlastmodified < vKillDate then
        count = ubound(arFilesToKill) + 1
        redim preserve arFilesToKill(count)
        set arFilesToKill(count) = file
      end if
    end if
  next


  if bIncludeSubFolders then
    for each fldr in folder.subfolders
      SelectFiles fldr.path,vKillDate,arFilesToKill,true
    next
  end if
end sub

No comments:

Post a Comment