批处理文件删除除最新的10个文件外的所有文件 [英] batch file remove all but the newest 10 files
本文介绍了批处理文件删除除最新的10个文件外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
批处理文件中包含以下内容:
I have the following in a batch file:
:REMOLDFILES
ECHO Removing files older than 14 days. >>%LOGFILE%
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%
IF [%3]==[Y] GOTO SECONDBACKUPDIR
IF [%3]==[y] GOTO SECONDBACKUPDIR
GOTO END
我遇到的问题是,备份没有运行两周,最终由于删除了两周以上而删除了所有备份.
The problem I ran in to is that the backup was not running for a couple of weeks and ended up deleting all of my backups since they were over 2 weeks old.
我需要的是保留最近的10个最新备份.
What I need is for it to keep the last 10 latest backups.
有人知道我该怎么做吗?我没有写过一次,因为我对批处理文件不太熟悉.
Anyone have an idea how I would do that? I did not write this once since I'm not that familiar with batch files.
推荐答案
在按最后修改日期排序后,您可以使用FOR /F SKIP
忽略最近修改的最后10个条目:
You could use FOR /F SKIP
to ignore the last 10 most recently modified entries after sorting by last modified date:
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d *.zip') do @del "%%F"
这篇关于批处理文件删除除最新的10个文件外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文