Our backup is performed by a sheduled batch job so eventually we will run out of space unless we remove our old backup files frequently.  Since I don't want to do this cleaning task manually I've decided to write an extra batch job that will delete all files older than x days.

At first I've created a javascript file (remove_old_files.js) that will run with two parameters: the number of days that we want to keep the backup files (files older than this number of days will be deleted), the second parameter is the path to the directory where the files and subfolders are stored. Please note that the deleting process is based upon the creation date of the files and folders!

//             REMOVE OLD BACKUP FILES                        //   
//        © FEBRUARY 2011 - Kenneth Van Baelen          //

function removeDays(date, nDays)
    var dateRet = date
    return dateRet.setDate(date.getDate() - nDays);

// Read arguments
var nDays = WScript.Arguments(0) || 21;
var Path = WScript.Arguments(1);

// Create system objects
var fs = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

// Compute date
var dateNow = new Date();
var dateTest = removeDays(dateNow, nDays);

// Iterate on files
var folder = fs.GetFolder(Path);
var sub = folder.subfolders;

for( var it = new Enumerator(sub); !it.atEnd(); it.moveNext() )
    var sub = it.item();

    if( sub.DateCreated < dateTest)

!!! For safety reasons the actual delete statement is set to comment, remove the // in front of the "sub.delete(true);" statement to reactivate the delete process. !!!

The second step is to create a batch file (remove_old_files.bat) where we call the javascript method to delete the files and subfolders of a certain path.

REM ******************************************************
REM *              REMOVE OLD BACKUP FILES                                                  *
REM *       © FEBRUARY 2011 -  Kenneth Van Baelen                                      *
REM ******************************************************

wscript "C:\PathToMyScript\REMOVE_OLD_FILES.js" 28 F:\\BACKUP\\FirstFolderThatNeedsToBeChecked
wscript "C:\PathToMyScript\REMOVE_OLD_FILES.js" 14 F:\\BACKUP\\SecondFolderThatNeedsToBeChecked
wscript "C:\PathToMyScript\REMOVE_OLD_FILES.js" 5 F:\\BACKUP\\ThirdFolderThatNeedsToBeChecked
from= This e-mail address is being protected from spambots. You need JavaScript enabled to view it to= This e-mail address is being protected from spambots. You need JavaScript enabled to view it body="The job to remove old backup files is successfully completed" subject="Remove old backup files" server=YourMailServer

In the example above the all the files and folder in directory F:\\BACKUP\\FirstFolderThatNeedsToBeChecked will be deleted if the creation date is older than 28 days from the current date.
The last thing we need to do is to create a sheduled task that will execute the remove_old_files.bat daily, weekly or monthly. Keep in mind that you have to put the smtpmail.exe file in the same folder as the script if you want the script to sent an e-mail confirmation after it's finished. More info about this executable can be found at http://www.smtpinfo.com/ 


Powered by: