Windows批处理文件删除X天之前的文件,但保留至少X个文件 [英] Windows batch file delete files older than X days but leave a min of X files

查看:68
本文介绍了Windows批处理文件删除X天之前的文件,但保留至少X个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些较旧的帖子,您可以在其中删除X天之前的文件,例如

I have see the older posts where you can delete files older than X days, such as this one Batch file to delete files older than N days. I'd like to add an additional filter to this so that if the backup I am running is not happening for 2 weeks, then it wouldn't delete all my backups.

我知道这一点:

forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

您可以删除X天之前的内容,但是如何添加上述条件,并让其在删除之前至少保留Z个文件?

That you can delete older than X days but how do I add the condition above and have it leave a minimum of Z files behind before it does the delete?

示例:

让我们说我要删除14天以上的文件,但以下文件应至少保留5个文件:

Lets say I want to delete files older than 14 days but keep a minimum of 5 files with the files below:

Jan 1 - backup1.zip
Jan 2 - backup2.zip
Jan 3 - backup3.zip
Jan 4 - backup4.zip
Jan 5 - backup5.zip
Jan 6 - backup6.zip
Jan 7-20 no backups done
Jan 21st - script runs and removed old files

使用示例代码删除所有早于14天的文件,我的所有备份文件都将被删除,而我将没有备份.我希望脚本能看到仅剩余6个文件,并至少保留5个文件.

With the example code that deletes all files older than 14 days, all of my backup files would be deleted and I'd be left with no backups. I'd like the script to see that only 6 files remains and keep a minimum of 5 files.

有道理吗?通过Windows批处理文件可以做到这一点吗?

Make sense? Is this possible through windows batch files?

为清晰起见

这是我的批处理文件的一部分,

This is part of my batch file that I have:

cd /d %BKUPDIR%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%

这些文件是我要删除的zip和日志文件,它们都位于同一文件夹中.

The files are zip and log files that I am deleting and they all reside in the same folder.

推荐答案

此脚本将删除所有早于14天的zip和日志文件,无论年龄如何,至少保留5个最新的zip和日志文件(共10个).

This script will delete all zip and log files older than 14 days, keeping a minimum of the 5 most recent of each (10 total), regardless of age.

请注意,FORFILES/D选项基于上次修改日期,因此我没有使用创建日期来订购DIR命令.

Note that the FORFILES /D option is based on last modified date, so I did not use creation date for ordering my DIR command.

FORFILES命令的运行速度很慢,因此,一旦我检测到文件的存在时间超过14天,就不必再运行FORFILES了,因为我知道所有剩余的文件都具有该年龄或更旧的文件.

The FORFILES command is quite slow, so once I detect the a file older than 14 days old, I don't bother running FORFILES again, since I know all remaining files are that age or older.

@echo off
pushd "c:\yourBackupPath"
for %%X in (zip log) do (
  set "skip=1"
  for /f "skip=5 delims=" %%F in ('dir /b /a-d /o-d /tw *.%%X') do (
    if defined skip forfiles /d -14 /m "%%F" >nul 2>nul && set "skip="
    if not defined skip del "%%F" 
  )
)  

这篇关于Windows批处理文件删除X天之前的文件,但保留至少X个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆