Windows script to remove old files

June 8th, 2011 by exhuma.twn

Simple script… still, I thought I’d share…

/**
* Remove all files and folders that are older than a set number of days.
*
* @param rootURI The URI of the root folder. All old files and folders in this
* folder are removed.
* @param days Files older than this number of days are deleted
*/
function purgeFiles(rootURI, days) {

  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var rootFolder = fso.GetFolder(rootURI);
  var subFolders = new Enumerator(rootFolder.SubFolders);

  // create a date before which files are considered "old"
  var threshold_date = new Date();
  threshold_date.setDate(threshold_date.getDate()-days);

  // loop over each file
  subFolders.moveFirst();
  for(;!subFolders.atEnd(); subFolders.moveNext()){
     var f = subFolders.item();
     if(f.DateCreated < threshold_date ){
        //WScript.Echo("Deleting "+f.Name+" last accessed on: "+f.DateCreated);
        f.Delete(true);
     }
  }

}

purgeFiles("C:/path/to/folder", 300);

view raw purgeFiles.js This Gist brought to you by GitHub.

Posted in Coding Voodoo | No Comments »

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Pages

Recent Posts

Categories

Links


Archives

Meta