【问题标题】:How to archive files older than 7 days with creating one archive for all files with same date?如何通过为相同日期的所有文件创建一个存档来存档超过 7 天的文件?
【发布时间】:2014-09-24 14:23:59
【问题描述】:

我正在寻找可以帮助我制定计划任务以自动将日志文件移动到 RAR 存档中的人。

不一定是批处理文件解决方案,如果您有其他想法请分享。

我得到了它的基本代码。这是我到目前为止的批处理文件代码:

"C:\Program Files\WinRAR\rar.exe" a -ag -ms "D:\tet\Web3811\Web3811\LogsBackup\" @backup.txt

批处理文件中的那一行运行 RAR 以创建一个存档,其中包含列表文件backup.txt 中指定的文件夹中的所有文件,其中包含:

D:\tet\Web3811\Web3811\log

RAR 存档在D:\tet\Web3811\Web3811\LogsBackup\ 中创建,文件名为yyyy-mm-dd.rar

我需要帮助:

  1. RAR 档案的日期应采用dd-mm-yyyy 格式而不是yyyy-mm-dd
  2. 仅应归档根据上次修改日期与当前日期相比超过 7 天的日志文件,因此时间无关紧要,只需日期即可。如果当前日期和时间是 2014 年 2 月 8 日 12:30:00,则所有日期和时间早于 27-07-2014 00:00:00 的文件都应添加到 RAR 存档中。
  3. 每个要创建的 RAR 压缩文件应仅包含具有相同最后修改日期的文件。
  4. RAR 压缩完成且无错误后,应删除所有归档日志文件。

之所以成为批处理文件,是因为需要作为定时任务执行。

第三个要求的例子:

该文件夹包含 5 个日志文件,最后修改日期如下:

Oldest.log    23-07-2014 02:20:54
AlsoOld.log   23-07-2014 23:52:26
Sample1.log   25-07-2014 09:08:46
Sample2.log   25-07-2014 12:59:02
Newest.log    26-07-2014 18:32:48

计划任务需要创建 3 个具有以下名称和文件的存档:

  1. 23-07-2014_Logs.rarOldest.logAlsoOld.log
  2. 25-07-2014_Logs.rarSample1.logSample2.log
  3. 26-07-2014_Logs.rarNewest.log

在 2014 年 7 月 24 日没有创建日志文件,因此也没有为这一天创建 RAR 存档。

【问题讨论】:

  • 使用存档名称中的时间也会给您单独的存档。
  • @foxidrive 它现在仍然会存档“log”文件夹中的所有内容,然后只需为 Rar 文件命名,包含日期和时间。
  • 计划任务应该多久执行一次?在午夜之后每天一次将使解决方案更容易编码,因为“超过 7 天”将仅匹配完全超过 7 天的文件。
  • 每天早上00:00执行一次,周末不执行。但是,如果有人在周末手动运行作业,则仍然可能有周末的日志文件。

标签: date batch-file task winrar


【解决方案1】:

我建议在批处理文件中使用:

"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!

【讨论】:

  • 太棒了,这正是我想要的,也解释得很好。这解决了我的问题。
  • 也可以在 7-zip 中执行此操作吗?基本上我只需要:"C:\Program Files\WinRAR\Rar.exe" mf -ep1 -idq -m5 -to7d -y "%BakDirectory%\!LastDate!_Logs.rar" "@%BakDirectory%\DayFiles.lst " 7-zip 格式,但也许 7-zip 没有所有需要的参数?如果你也可以添加它,你应该得到一颗金星 :)
  • 我不使用 7-Zip,因此不知道 7-Zip 是否有类似于 -to7d 的开关并返回一个退出代码,让批处理文件知道没有符合条件的文件用于归档。但是,如果您想创建 ZIP 而不是 RAR 压缩文件,请将 Rar.exe 替换为 WinRAR.exe 并将 .rar 替换为 .zip,然后 WinRAR 将创建 ZIP 压缩文件。此外,开关-afzip 可以插入到 WinRAR 命令行中,这样 WinRAR 就不需要根据存档文件的文件扩展名来确定存档格式来创建。 Rar.exe 不支持 ZIP 存档,因此也无法切换 -afzip
  • 好吧,其实是因为大部分人没有Winrar授权,所以如果也能用免费的压缩软件来搞定就好了。
猜你喜欢
  • 2014-10-05
  • 2018-10-29
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多