如何归档超过7天以上的文件,对于同一日期的所有文件创建一个档案? [英] How to archive files older than 7 days with creating one archive for all files with same date?

查看:248
本文介绍了如何归档超过7天以上的文件,对于同一日期的所有文件创建一个档案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找人谁可以帮我做一个计划任务自动移动日志文件到RAR压缩包。

I am looking for someone who can help me make a scheduled task to automatically move log files into RAR archives.

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

我得到了基本的code吧。这是批处理文件code我到目前为止有:

I got the basic code for it. This is the batch file code I have so far:

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

在批处理文件运行该行的 RAR ,以创建具有列表文件中指定的文件夹中的所有文件为Backup.txt 包含档案:

That line in the batch file runs RAR to create an archive with all files in the folder specified in list file backup.txt containing:

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天以上。与27-07-2014 00:00:00之前的日期和时间,所有的文件应该被添加到RAR压缩文件,如果当前日期和时间是2014年2月8日12:30:00。

  3. 每个RAR压缩文件创建应该包含相同的最后修改日期的文件。

  4. 一旦RAR COM pression是无错完成所有的归档日志文件应该被删除。

  1. The RAR archives should have date in format dd-mm-yyyy in name instead of yyyy-mm-dd.
  2. Only log files should be archived which are older than 7 days according to last modification date in comparison to current date whereby time does not matter, just date. All files with a date and time before 27-07-2014 00:00:00 should be added to the RAR archives if current date and time is 02-08-2014 12:30:00.
  3. Each RAR archive to create should contain only files with same last modification date.
  4. All archived log files should be deleted once the RAR compression is completed without errors.

原因是一个批处理文件是被作为执行计划任务的要求。

The reason for being a batch file is the requirement of being executable as scheduled task.

第三要求的一个例子:

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

The folder contains 5 log files with following last modification dates:

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档与下列名称和文件:

The scheduled task needs to create 3 archives with following names and files:


  1. 23-07-2014_Logs.rar Oldest.log AlsoOld.log

  2. 25-07-2014_Logs.rar Sample1.log Sample2.log

  3. 26-07-2014_Logs.rar 只有 Newest.log

  1. 23-07-2014_Logs.rar with Oldest.log and AlsoOld.log.
  2. 25-07-2014_Logs.rar with Sample1.log and Sample2.log.
  3. 26-07-2014_Logs.rar with just Newest.log.

没有日志文件的创建24-07-2014,因此也没有RAR压缩文件创建为这一天。

No log file was created on 24-07-2014 and therefore also no RAR archive to create for this day.

推荐答案

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

I suggest to use in the batch file:

"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压缩包文件要求的格式,并在运行此命令行多次在第一天的情况下,连字符后开始数1额外的递增数。

Above command moves all files older than 7 days according to last modification date into a RAR archive with name starting with Logs_ and current date in requested format and an additional incrementing number starting with number 1 after a hyphen in case of running this command line several times on one day.

只有存档属性的文件移动到归档。存档属性存档即使当其他应用程序已打开了写锁文件删除是不可能的,例如文件后清空。 RAR 不删档上阅读和COM pressing在所有数据失败(读锁)。

Only files with archive attribute are moved into an archive. The archive attribute is cleared after archiving a file even if deletion is not possible for example when another application has opened the file with a write lock. RAR does not delete files on which reading and compressing the data failed at all (read lock).

见程序文件的 WinRAR的文件夹中的文本文件的 Rar.txt 在此命令行中使用的所有交换机的描述。

See text file Rar.txt in program files folder of WinRAR for a description of all the switches used in this command line.

在一些要求已经解释的更好,这里是一个批处理文件来创建归档文件作为最终要求。

After some requirements have been explained better, here is a batch file to create the archive files as finally requested.

@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 ,因为它可以上面看到。

I first thought, it is not possible to do this without coding a small console application to create the file lists per date and ignore files not modified within the last 7 days. But then I had an idea on how to solve this main problem using just a batch file and RAR as it can be seen above.

这是最好的午夜后短期运行有预定任务此批处理文件的 RAR 也需要当前时间考虑为超过7天,而不仅仅是日期。

It is best to run this batch file with a scheduled task short after midnight as RAR takes also current time into account for "older than 7 days" and not just the date.

但是,如果批处理文件,在18:00例如执行,有创建于23:00分别修改日志文件,这将是没有问题的。在比较当前日期的最后修改日期这种情况下,日志文件,在18:00前,用准确日期前7天首先搬进了RAR压缩包,并在第二天的其他日志文件最后在18:00之后从相同的修改日期也移到这个日期的RAR压缩文件。

But it would be no problem if batch file is executed for example at 18:00 and there are log files created respectively modified at 23:00. In this case log files with last modification date before 18:00 and with a date exactly before 7 days in comparison to current date are moved first into a RAR archive, and on next day the other log files last modified after 18:00 from same date are moved also to the RAR archive for this date.

批处理任务实例18:00总是执行,并发生了什么。

Example with batch task executed always at 18:00 and what happens.

有日志文件

FirstSundayAugust2014_1.log   03/08/2014 15:23
FirstSundayAugust2014_2.log   03/08/2014 23:48

和计划任务上周日,2014年8月10日18:00运行。

and the scheduled task runs on Sunday, 10th August 2014 at 18:00.

该批处理文件移动 FirstSundayAugust2014_1.log 成RAR压缩文件 03-08-2014_Logs.rar ,但对方日志文件 FirstSundayAugust2014_2.log 从上周日也仍然在目录中。

The batch file moves FirstSundayAugust2014_1.log into RAR archive 03-08-2014_Logs.rar, but the other log file FirstSundayAugust2014_2.log also from last Sunday remains in the directory.

在周一,2014年8月11日18:00批处理文件也移动 FirstSundayAugust2014_2.log 成RAR压缩包 03-08-2014_Logs。 RAR 现在这个压缩文件包含分别创建最后修改于第一个星期日八月这两个日志文件2014年。

On Monday, 11th August 2014 at 18:00 the batch file moves also FirstSundayAugust2014_2.log into the RAR archive 03-08-2014_Logs.rar and this archive contains now both log files created respectively last modified on first Sunday in August 2014.

还要说明一点:

RAR与日期格式DD-MM-YYYY文件名不是我的观点非常好。更好的是YYYY-MM-DD,因为这将导致在那些RAR文件,根据文件名在Windows资源管理器会导致同一名单按字母顺序排列* .rar文件当这些RA​​R文件是根据文件的日期/时间在Windows资源管理器上市

RAR file names with date in format DD-MM-YYYY are not really good in my point of view. Better would be YYYY-MM-DD as this results in *.rar files where those RAR files listed alphabetically according to file name in Windows Explorer would result in same list as when those RAR files are listed according to file date/time in Windows Explorer.

要获得RAR文件格式有YYYY-MM-DD的日期在文件名行

To get RAR files with format YYYY-MM-DD for the date in file name the line

set FileDate=!FileTime:~0,2!-!FileTime:~3,2!-!FileTime:~6,4!

需要进行修改,以

needs to be modified to

set FileDate=!FileTime:~6,4!-!FileTime:~3,2!-!FileTime:~0,2!

这篇关于如何归档超过7天以上的文件,对于同一日期的所有文件创建一个档案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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