I’ve been working for a client over recent months and part of my resposibility has been to look after a number of servers used for performance testing. Occasionally drives fill up on the server farm causing outages and obvious interruptions to testing.
It would be a near full-time job to manage all these servers, so to reduce the chance of temporary files from filling up drives and causing problems, I looked into creating a scheduled task to delete old temporary files. Since my server estate is varied, I didnt want to use PowerShell so I opted for an old “DOS” command, FORFILES.
This seems to have done the trick for me:
forfiles /s /m *.* /d -7 /c "cmd /c del @path"
(This command deletes all files that are more than 7 days old from the folder in which it runs.)
Update – To remove folders as well:
forfiles /S /D -7 /C "cmd /c IF @isdir == TRUE RMDIR @path /S /Q"
(This command deletes all folders that are more than 7 days old from the folder in which it runs. – Run it after deleting the files with the command above.)