Windows批处理文件只保留最后的30个文件 [英] Windows batch file only keeping the last 30 files

查看:1123
本文介绍了Windows批处理文件只保留最后的30个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写一个批处理文件,执行以下操作:

I have written a batch file that does the following:

REM @ECHO OFF
rem %1 = coid
rem %2 = rpg location with ending /

rem get the path of this cmd script
SET SUBDIR=%~dp0

SET BKUPDIR=%SUBDIR%BACKUPS\
SET LOGFILE=%BKUPDIR%backup.log

ECHO ************************************************* >>%LOGFILE%
ECHO STARTING BACKUP FOR %1 AT %2 >>%LOGFILE%

FOR /f "skip=1" %%x in ('wmic os get localdatetime') DO IF NOT defined mydate SET mydate=%%x
SET filedate=%mydate:~0,14%
SET fullname=%BKUPDIR%%1_%FILEDATE%.ZIP

ECHO BKUPDIR: %BKUPDIR% >>%LOGFILE%
ECHO mydate: %mydate% >>%LOGFILE%
ECHO filedate: %filedate% >>%LOGFILE%
ECHO fullname: %fullname% >>%LOGFILE%
ECHO SUBDIR: %SUBDIR% >>%LOGFILE%

rem lets make sure the backup directory exists before starting
IF NOT EXIST %BKUPDIR% MD %BKUPDIR% >>%LOGFILE%

%SUBDIR%7z a -tzip %fullname%  %2%1*.d
IF ERRORLEVEL 255 ECHO user_stopped_the_process >>%LOGFILE%
IF ERRORLEVEL 8 ECHO not_enough_memory >>%LOGFILE%
IF ERRORLEVEL 7 ECHO command_line_error >>%LOGFILE%
IF ERRORLEVEL 2 ECHO fatal_error >>%LOGFILE%
IF ERRORLEVEL 1 ECHO ok_warnings >>%LOGFILE%

IF ERRORLEVEL 0 GOTO END
IF EXIST %fullname% DEL %fullname%

:END
ECHO FINISHING BACKUP FOR %1 >>%LOGFILE%
ECHO ************************************************* >>%LOGFILE%
set mydate=

我保存文件作为%1 和日期/时间创建文件:

I am saving the files as the %1 and the date/time the file was created:

SSS_20130110133304.ZIP 
SSS_20130110133336.ZIP

我的任务调度运行此运行每一个夜晚。

I am running this in the task scheduler to run every night.

我想从目录中有太多的zip文件保留,所以我想继续存在的最后30 zip文件。

I want to keep from having too many zip files in the directory so I'd like to keep the last 30 zip files that exist.

我被困在这一点上。如何保持最近的30个zip文件,这样我不Zip文件废话负荷结束了?

I'm stuck at this point. How do I keep the most RECENT 30 zip files so that I don't end up with a crap load of zip files?

推荐答案

这将列出.zip文件从最新到最旧(按创建日期),跳过第30个文件。

This will list the .zip files from newest to oldest (by creation date), skipping the first 30 files.

for /f "skip=30 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.zip ^2^>nul') do if exist "%%~fA" echo "%%~fA"

只要改变回声删除当你想真正删除文件。 :)

Just change the echo to del when you want to actually delete the files. :)

这篇关于Windows批处理文件只保留最后的30个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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