Wednesday, May 5, 2010

VB Script - Delete Files Older then X Days

I whipped up handy VB Script that will delete files older then X days in any particular folder. In this case any files that are older then 3 days will be deleted.

Dim Fso, Directory, Modified, Files

Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder("C:\FolderContainingFiles")
Set Files = Directory.Files
For Each Modified in Files
If DateDiff("D", Modified.DateLastModified, Now) > 3 Then Modified.Delete
Next

2 comments:

  1. Nice clean script, works a charm.

    Thx for providing!

    ReplyDelete
  2. Perfect! I have a program that backs up to a new file every day. This keeps just the last 30 days. Works great.

    ReplyDelete