Codewarp Home
Login | Site Map
Support

Dedicated Knowledge Base


Article 174
Title VBScript to remove old files
Content


Option Explicit

Dim arg
Set arg = WScript.Arguments

Dim sh
Set sh = CreateObject("WScript.Shell")

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

Dim iDeleted
iDeleted = 0

Dim iSkipped
iSkipped = 0

If arg.Count < 3 Then
  DisplayHelpMessage
  WScript.Quit(-1)
Else
  Dim sFolder
  sFolder = arg.Item(0)
WScript.Echo("Folder: " & sFolder)

  Dim sPattern
  sPattern = arg.Item(1)
WScript.Echo("Pattern: " & sPattern)

  Dim sAge
  sAge = arg.Item(2)

  Dim t
  t = CDate(Now - CInt(sAge))
WScript.Echo("Age: " & t)
WScript.Echo()

  Dim folder
  Set folder = fso.GetFolder(sFolder)

  Dim file
  For Each file in folder.Files
    If CDate(file.DateLastModified) < t Then
      If sPattern = "*" Or InStr(file.Name, sPattern) > 0 Then
'WScript.Echo(file.Name & " " & file.DateLastModified & " deleted.")
        file.Delete
        iDeleted = iDeleted + 1
      Else
'WScript.Echo(file.Name & " " & file.DateLastModified & " " & sPattern & " skipped.")
        iSkipped = iSkipped + 1
      End If
    Else
'WScript.Echo(file.Name & " " & file.DateLastModified & " skipped.")
      iSkipped = iSkipped + 1
    End If
  Next
End If

WScript.Echo(iDeleted & " deleted.")
WScript.Echo(iSkipped & " skipped.")

Sub DisplayHelpMessage()
  WScript.Echo
  WScript.Echo "Usage:"
  WScript.Echo "  RM_OLD.VBS <folder> [<age>]"
  WScript.Echo
  WScript.Echo "Description:"
  WScript.Echo "  RM OLD tool removes files based on age in days since last modified"
  WScript.Echo
  WScript.Echo "Samples:"
  WScript.Echo "  rm_old.vbs c:\temp 10"
End Sub

Modified Date 3/28/2006

    Your feedback is important to us. Please send us comments:

    Contact Us Contact Us