如何通过为具有相同日期的所有文件创建一个存档来使用 7-ZIP 存档超过 7 天的文件 [英] How to archive files older than 7 days with 7-ZIP by creating one archive for all files with same date

查看:15
本文介绍了如何通过为具有相同日期的所有文件创建一个存档来使用 7-ZIP 存档超过 7 天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有一个 CLOSE 解决方案,所有内容都在这个问题中描述:如何通过为所有具有相同日期的文件创建一个存档来存档 7 天以上的文件?

There is a CLOSE solution to my problem, and everything is described in this question: How to archive files older than 7 days with creating one archive for all files with same date?

问题是,我需要另一个与此类似但适用于 7-Zip 的解决方案,我相信这必须在 bat 文件中编码,因为 7-Zip 中没有像 Winrar 中那样的 -to7d 开关.

The thing is, i need another solution similar to this but working for 7-Zip, this has to be coded in the bat file i believe since there is no -to7d switch in 7-Zip like there is in Winrar.

现在的代码(感谢@Mofi 创建此代码):

The Code right now ( Credit goes to @Mofi for creating this ):

@echo off

setlocal EnableExtensions EnableDelayedExpansion
rem Define the directories to use for backup task.
set "LogDirectory=D:	etWeb3811Web3811log"
set "BakDirectory=D:	etWeb3811Web3811LogsBackup"

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-Zip7z" -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 就会带走那个文件夹中的所有东西,据我所知,没有任何开关可以检查.

Basically there just needs to be added a check that check if Modified date = older than 7 days. i'm not sure how to implement this, since once you run the :ArchiveLogs 7-Zip will just take EVERYTHING in that folder and as far as i can see there are no switches to check for that.

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

I believe we have to do something like this:

  • 用于检查文件修改日期是否早于 7 天的代码将该文件名保存在一个值中.

:ZipOnlyafter7days
对于:
"C:Program Files (x86)7-Zip7z" -sdel a -mmt -mx3 -tzip "%BakDirectory%!LastDate!_Logs.zip" "@%BakDirectory% Filename_Value"

:ZipOnlyafter7days
For:
"C:Program Files (x86)7-Zip7z" -sdel a -mmt -mx3 -tzip "%BakDirectory%!LastDate!_Logs.zip" "@%BakDirectory% Filename_Value "

推荐答案

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

You should use robocopy with the /maxage:7 option to copy relevant files to a temp folder, and then compress all the files using 7zip.

这篇关于如何通过为具有相同日期的所有文件创建一个存档来使用 7-ZIP 存档超过 7 天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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