如何使用FORFILES(或类似)删除文件早于n天,但总是留下最近ñ [英] How to use forfiles (or similar) to delete files older than n days, but always leaving most recent n
问题描述
(使用Windows 2000和2003服务器)
我们使用forfiles.exe删除N天较旧的备份.zip文件,它的伟大工程(命令有点像下图)
FORFILES -pC:\\备份-m*。ZIP-ccmd / c如果@ ISDIR == FALSE德尔\\@ PATH \\ @FILE \\ D-5
如果一个.zip文件创建失败,我想确保我们不会在5天的备份0 .zip文件结束。因此,该命令需要:
删除任何超过5天的,但始终保持最新的5个文件,即使他们自己都超过5天的
我们可以使用forfiles.exe或另一种解决方案(尽管任何一个华而不实的单行是始终preferable到一个脚本文件)。
谢谢!
FOR / F跳过= 5 delims =%% G IN('DIR / B / OD / A- D')DO DEL%% G
将删除除5最新以外的所有文件。我无法找到一个班轮将所有的文件的更新超过5天所以你可能需要使用一些更复杂的逻辑。
/ B
仅列出文件名没有额外的信息
/ O-D
通过反向日期顺序排序列表。
/ A-D
过滤器只显示非目录文件
跳过= 5
跳过第5线(5最新的)。
(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!
FOR /F "skip=5 delims=" %%G IN ('dir /b /O-D /A-D') DO del "%%G"
Will delete all files except the 5 newest ones. I couldn't find a one-liner to keep all files newer than 5 days so for that you might have to use some more complicated logic.
/b
Lists only file names without extra info
/O-D
Sorts list by reverse date order.
/A-D
Filters to only show non-directory files
skip=5
skips the 5 first lines (5 newest ones).
这篇关于如何使用FORFILES(或类似)删除文件早于n天,但总是留下最近ñ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!