Delete all files X days old
Lately, after reading the Pragmatic Programmer, I have tried to follow the DRY principle whenever I can. One thing that I have to do every so often is clean up log files that are X many days old. Today when I had to do that again I found this bit of command-line goodness to solve that issue on Scott Elkin’s blog:
Forfiles -p c:\path\to\backups -s -m *.* -d -5 -c "cmd /c del /q @path"
This will delete all files older than 5 days in that path. You have several options with the flags if you want to delete other time periods. Also, check out Scott’s post for details on how to display what files will be delete so you can make sure you don’t delete last nights backup. One thing to note is that you have to run this on a 2003 box as XP does not support the ForFiles command.
- Posted by Kale at 07:27 pm
- Permalink for this entry
- Filed under: General
- RSS comments feed of this entry
- TrackBack URI
Kale,
that was really odd. I got your linked in invite and then decided to check out your website and your very first post links to Scott Elkin, who I worked with for a while a few years back!