In my last post I covered how to back-up files based on age. This is a nice script to supplement it, this will auto-delete files based on their age. It’s an easy way to clean up old files from an automated task that saves files to disk. No third-party software needed!!
This example is for a Windows host, to delete a file older than 3 days.
1. Create a batch file with the following:
echo on rem Delete files older than 3 days FORFILES /P C:\Admin\Test\ /S /M 1*.bmp /D -3 /c " CMD /c del /q @FILE "
2. Modify the following flags:
/p = The path to search for the files you want to check the date of and remove
/s = Recurse subdirectories contained within the path specified using /p and check them as well
/m = The search mask to be used for the file type you want to check the date on (*.* being all files)
/d = The date to compare the files against. A standard date type can also be used (dd/mm/yyyy)
/c = The command to be used on a file that matches the /m and /d criteria
/q = Used within /c to instruct the del command to delete files quietly
3. Add the batch file to run with task scheduler based on your needs.