Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created June 8, 2011 09:23
Show Gist options
  • Save exhuma/1014084 to your computer and use it in GitHub Desktop.
Save exhuma/1014084 to your computer and use it in GitHub Desktop.
Windows script to remove old files
/**
* 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment