I have a server that is continuously filling it’s drive with temporary files left over from a video encoding process we use. Every week or so I have to manually delete the old files to prevent the drive from filling. Finally today I headed on a journey to figure out a way to programmatically clean up these old files regularly. So I tooled around the Internet to find a way to do it from the DOS command line. Unfortunately others had run into the same issue I was, that DOS commands like del don’t have a way to delete by date. I found a few approaches using complex batch files & even tried one for a few minutes, but when I couldn’t get it to work I went back to the drawing board.
I found a really old post suggesting using xcopy to copy the newest (presumably the ones to keep) files to a temp location, then delete the original files, then move those copied away back to their original location. This approach had some promise, but had some real drawbacks too; particularly that I’m dealing with tens of gigabytes & this would take forever, and that I’m dealing with several and varying subdirectories.
Since xcopy has been deprecated and replaced with robocopy in Windows 2008, Windows 7, etc. that’s what I chose to use. In fact, robocopy has a couple switches that make it easy to move (/MOV) files older than x days (/MINAGE:x).
What I ended up with was a simple two line batch file that I’m using Windows task scheduler to run once a day. The first line moves files older than x days to a temporary location & the second line deletes the moved files.
robocopy D:Original_Location D:DelOldFiles * /S /MOV /MINAGE:7
del D:DelOldFiles* /s /q
The robocopy syntax is ROBOCOPY source destination [file [file]…] [options]. I’m using the following switches (options):
- /S – include subdirectories
- /MOV – move (which is technically a copy, then delete original)
- /MINAGE:x – act on files older than x days