我建议在批处理文件中使用:
"C:\Program Files\WinRAR\rar.exe" mf -ac -ao -agDD-MM-YYYY-NN -ep1 -idq -m5 -to7d -y "D:\tet\Web3811\Web3811\LogsBackup\Logs_" @backup.txt
上述命令移动根据上次修改日期超过 7 天的所有文件到一个 RAR 存档中,名称以 Logs_ 开头,当前日期为请求格式,以及一个以数字 1 开头的附加递增数字如果在一天内多次运行此命令行,则在连字符之后。
只有具有归档属性的文件才会被移动到归档中。归档文件后,即使无法删除(例如,当另一个应用程序使用写锁打开文件时),归档属性也会被清除。 RAR 不会删除读取和压缩数据完全失败的文件(读取锁定)。
查看WinRAR的程序文件夹中的文本文件Rar.txt,了解此命令行中使用的所有开关的说明。
在对一些要求进行了更好的解释之后,这里是一个批处理文件,用于根据最终要求创建存档文件。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Define the directories to use for backup task.
set "LogDirectory=D:\tet\Web3811\Web3811\log"
set "BakDirectory=D:\tet\Web3811\Web3811\LogsBackup"
rem Get all file names in log directory into a list file sorted by last
rem modification date with oldest file at top and newest at bottom.
rem Note: /S is important to get the file names with complete path.
dir "%LogDirectory%\*" /A-D /B /OD /S /TW 1>"%BakDirectory%\LogFiles.lst" 2>nul
rem Jump to clean up stage if no file found in the log directory.
if errorlevel 1 goto :CleanUp
rem Delete list file for all files with same day if file exists
rem for example from a previous execution of this batch file
rem which was terminated manually by a user during execution.
if exist "%BakDirectory%\DayFiles.lst" del "%BakDirectory%\DayFiles.lst"
set LastDate=none
for /F "usebackq delims=" %%F in ( "%BakDirectory%\LogFiles.lst" ) do (
set FileTime=%%~tF
rem Get just file date from file time in format DD-MM-YYYY.
rem The file time string format depends on date and time
rem format definition in Windows language settings.
rem Therefore the line below must be adapted if date format
rem is whether DD.MM.YYYY nor DD-MM-YYYY nor DD/MM/YYYY.
set FileDate=!FileTime:~0,2!-!FileTime:~3,2!-!FileTime:~6,4!
rem Is the last modification date of this file different
rem to last modification date of the previous file?
if not "!FileDate!"=="!LastDate!" (
rem Nothing to archive on first difference.
if not "!LastDate!"=="none" call :ArchiveLogs
rem Exit loop if RAR has not archived any file which means
rem all other files are modified within the last 7 days.
if "!LastDate!"=="ExitLoop" goto CleanUp
rem Start creating a new list.
set LastDate=!FileDate!
)
rem Append name of this file with path to current day list.
echo %%F>>"%BakDirectory%\DayFiles.lst"
)
rem Jump to clean up stage if no list file with files to archive.
if not exist "%BakDirectory%\DayFiles.lst" goto CleanUp
rem Otherwise with no log file created or modified within
rem the last 7 days, but at least one older file exists
rem nevertheless, archive all those files in list file.
call :ArchiveLogs
:CleanUp
del "%BakDirectory%\LogFiles.lst"
endlocal
goto :EOF
:ArchiveLogs
rem Move all files in the list file older than 7 days without
rem path using best compression into a RAR archive with last
rem modification date of archived file(s) in RAR file name.
"C:\Program Files\WinRAR\Rar.exe" mf -ep1 -idq -m5 -to7d -y "%BakDirectory%\!LastDate!_Logs.rar" "@%BakDirectory%\DayFiles.lst"
rem Exit FOR loop above if no file archived because
rem no file in the list file is older than 7 days.
if errorlevel 10 set LastDate=ExitLoop
del "%BakDirectory%\DayFiles.lst"
我首先想到的是,如果不编写一个小型控制台应用程序来创建每个日期的文件列表并忽略过去 7 天内未修改的文件,就不可能做到这一点。但后来我想到了如何仅使用批处理文件和 RAR 来解决这个主要问题,如上所示。
最好在午夜后不久使用计划任务运行此批处理文件,因为 RAR 还会将当前时间考虑为“超过 7 天”,而不仅仅是日期。
但是如果批处理文件例如在 18:00 执行并且在 23:00 分别创建和修改日志文件,则没有问题。在这种情况下,最后修改日期在 18:00 之前且与当前日期相比日期正好在 7 天之前的日志文件首先被移动到 RAR 存档中,然后在第二天 18:00 之后最后修改的其他日志文件从同一日期也会移动到该日期的 RAR 存档中。
批处理任务总是在 18:00 执行的示例以及会发生什么。
有日志文件
FirstSundayAugust2014_1.log 03/08/2014 15:23
FirstSundayAugust2014_2.log 03/08/2014 23:48
计划任务在 2014 年 8 月 10 日星期日 18:00 运行。
批处理文件将FirstSundayAugust2014_1.log 移动到RAR 存档03-08-2014_Logs.rar,但同样来自上周日的另一个日志文件FirstSundayAugust2014_2.log 仍保留在目录中。
在 2014 年 8 月 11 日星期一 18:00,批处理文件也将 FirstSundayAugust2014_2.log 移动到 RAR 存档 03-08-2014_Logs.rar 中,并且此存档现在包含分别在 2014 年 8 月的第一个星期日创建的两个日志文件。
还有一点:
在我看来,日期格式为 DD-MM-YYYY 的 RAR 文件名并不是很好。最好是 YYYY-MM-DD,因为这会生成 *.rar 文件,其中那些 RAR 文件根据 Windows 资源管理器中的文件名按字母顺序列出的结果与这些 RAR 文件在 Windows 资源管理器中根据文件日期/时间列出时的列表相同.
要获取格式为 YYYY-MM-DD 的 RAR 文件,文件名中的日期为该行
set FileDate=!FileTime:~0,2!-!FileTime:~3,2!-!FileTime:~6,4!
需要修改为
set FileDate=!FileTime:~6,4!-!FileTime:~3,2!-!FileTime:~0,2!