Howto: automatically remove files older than ‘x’ days

by admin on June 12, 2007

Tape backups have failed me too many times, so I now do my Windows backup to an external hard drive. One of the office staff is in charge of swapping out our backup drives and taking it offsite. I needed a solution that would remove old backups without user intervention so they wouldn’t have to worry about having enough space available for the backup to complete.

My hard drives can hold four backups, so here’s a very simple .vbs script that deletes files from I:\Backup Files that are more than three days old:

Dim Fso

Dim Directory

Dim Modified

Dim Files

Set Fso = CreateObject(“Scripting.FileSystemObject”)

Set Directory = Fso.GetFolder(“I:\Backup Files”)

Set Files = Directory.Files

For Each Modified in Files

If DateDiff(“D”, Modified.DateLastModified, Now) > 3 Then Modified.Delete


Save this as filename.vbs

To execute this file, run:

cscript.exe filename.vbs

Thanks to Don for the original script I modified for my particular needs.

[updated 12-07-2007]

I just added another script that automatically removes files older than ‘x’ days which uses the forfiles command that is native to Windows.

[updated 01-01-2008]

Here’s another simple script from KWSupport.

[updated 01-02-2008]

This script from the Scripting Guy deletes files ‘X’ number of hours old. Looks like it could be modified easily to meet your needs.

Previous post:

Next post: