如何通过为具有相同日期的所有文件创建一个存档,使用7-ZIP存档7天以上的文件

时间:2014-08-12 07:46:45

标签: batch-file zip task 7zip rar

我的问题有一个CLOSE解决方案,所有内容都在这个问题中描述: How to archive files older than 7 days with creating one archive for all files with same date?

问题是,我需要另一种类似于此的解决方案但是为7-Zip工作,这必须在bat文件中编码我相信因为在Win-7中没有7-Zip中的-to7d开关。< / p>

现在的代码(来自@Mofi创建此代码):

@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:~6,4!-!FileTime:~3,2!-!FileTime:~0,2!

   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
"C:\Program Files (x86)\7-Zip\7z" -sdel a -mmt -mx3 -tzip "%BakDirectory%\!LastDate!_Logs.zip" "@%BakDirectory%\DayFiles.lst"

if errorlevel 10 set LastDate=ExitLoop
del "%BakDirectory%\DayFiles.lst"

基本上只需要添加一个检查,检查修改日期是否超过7天。 我不知道如何实现这一点,因为一旦你运行:ArchiveLogs 7-Zip将只占用该文件夹中的所有内容,据我所知,没有开关来检查它。

我相信我们必须做这样的事情:

  • 检查文件的修改日期是否超过7天的代码&amp;将文件名保存为值
  

:ZipOnlyafter7days
      适用于:
      &#34; C:\ Program Files(x86)\ 7-Zip \ 7z&#34; -sdel a -mmt -mx3 -tzip&#34;%BakDirectory%!LastDate!_Logs.zip&#34; &#34; @%BakDirectory%\ Filename_Value &#34;

2 个答案:

答案 0 :(得分:0)

您应该使用robocopy和/ maxage:7选项将相关文件复制到临时文件夹,然后使用7zip压缩所有文件。

答案 1 :(得分:0)

以下适用于批处理文件中具有天数的文件

forfiles /P (SOURCE_folder) /S /M *.* /D -(number of date old) /C "cmd /C 7Z.exe a "Archive.ZIP @file" 

我正在努力尝试让日期正确。