How to archive files older than 7 days with 7-ZIP

2019-09-09 11:01发布

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?

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.

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:\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"

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:

  • Code to check if file has a modification date older than 7 days & save that file name in a value.

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

2条回答
聊天终结者
2楼-- · 2019-09-09 11:17

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.

查看更多
干净又极端
3楼-- · 2019-09-09 11:33

The below works for files with a number of days old from a batch file

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

I am just working to try and get the date correct.

查看更多
登录 后发表回答