Skip to content
Snippets Groups Projects
Commit ba78af3e authored by rswindell's avatar rswindell
Browse files

Useful utility script to automatically purge files in any directory older than

a certain age in days (defaults to 30).
parent 29c6f1f3
No related branches found
No related tags found
No related merge requests found
// purgefiles.js
const default_max_age=30; // days
argn=0;
if((max_age=parseInt(argv[argn]))>0)
argn++;
else
max_age=default_max_age;
if(argc==argn) {
printf("\nusage: purgfiles.js [max_age_in_days] <directory/pattern>\n");
printf("\ndefault max_age_in_days = %lu\n",default_max_age);
exit();
}
printf("Getting directory of %s", argv[argn]);
files = directory(argv[argn]);
printf("\n%lu files\n",files.length);
printf("\nPurging files aged more than %lu days\n",max_age);
purged_files=0;
now = time();
for(i in files) {
t=file_date(files[i]);
age=(now-t)/(24*60*60);
if(age <= max_age)
continue;
print(system.datestr(t) + " " + files[i]);
if(!file_remove(files[i])) {
log("!Error " + errno + " removing " + files[i]);
continue;
}
purged_files++;
}
printf("%lu files purged.\n",purged_files);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment