How to use forfiles (or similar) to delete files older than n days, but always leaving most recent n
Posted
by Aidan Whitehall
on Stack Overflow
See other posts from Stack Overflow
or by Aidan Whitehall
Published on 2010-04-22T13:37:47Z
Indexed on
2010/05/03
12:48 UTC
Read the original article
Hit count: 323
(Using Windows 2000 and 2003 Server)
We use forfiles.exe to delete backup .zip files older than n days, and it works great (command is a bit like below)
forfiles -p"C:\Backup" -m"*.zip" -c"cmd /c if @ISDIR==FALSE del \"@PATH\@FILE\"" -d-5
If a .zip file fails to be created, I'd like to ensure that we don't end up with 0 .zip files in the backup after 5 days. Therefore, the command needs to be:
"delete anything older than 5 days, but ALWAYS keep the most recent 5 files, EVEN if they themselves are older than 5 days"
We can use forfiles.exe or another solution (although anything that is a slick one-liner is ALWAYS preferable to a script file).
Thanks!
© Stack Overflow or respective owner