Using forfiles.exe to delete old files…

August 4, 2012 · Posted in Geekness 

So I setup a PC DVR unit to handle some security cameras for a client of mine.  As a result, the *.avi files would start to eat up a lot of space after a month or so of recording.  So I elected to use the forfiles.exe program to take care of the problem.  Note this is a windows platform and if you are using anything pre-W2k3 (like XP), you’ll need to download the program from MS (link is for x86!).

For simplicity, I just saved it in c:\windows.  Next you’ll need to figure out the proper syntax to use for your needs.  Mine was to delete any .AVI file that was older than 7 days, located in a particular pathway, such as c:\dvr-video.  Below is what I am using…

FORFILES -pc:\dvr-video-s -m*.avi -d-7 -c”CMD /c del /q @FILE”

I would suggest you test this prior to blasting away your files.  The line below will echo all the files that would normally be deleted if you ran the command above.

FORFILES -pc:\dvr-video -s -m*.avi -d-7 -c”CMD /c ECHO @FILE”

I would also suggest you take a look at the FORFILES syntax flags. Here is a link to the CMD syntax flags too, if you are new to the command.  Once you have it all worked out, save it in a notepad file with a .bat extension.  Location doesn’t matter, just somewhere you can find it for the next step.

The final step is to fire up your Task Scheduler and configure it to run this batch file however often you need it to run.  Since my batch file deletes files older than 7 days, I run it once a week on Saturday, at 4AM.

Overall, it is a pretty simple setup.

2 things to note…

1. If your files have spaces in them, this won’t work.  The only workaround I know offhand is to use underscore or hyphens in your files names.

2. If there are spaces in your pathway, you’ll need to put it in quotes, like FORFILES -p”c:\my pathway\has spaces\to my\files”


Comments are closed.

  • Randoms


    Views: 591

  • Latest Geek MPG

  • Phoenix