【发布时间】:2014-10-05 04:46:50
【问题描述】:
我的问题有一个 CLOSE 解决方案,所有内容都在这个问题中描述: 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 开关。
现在的代码(感谢 @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 天的修改日期并将该文件名保存在值中的代码。
:ZipOnlyafter7days
对于:
"C:\Program Files (x86)\7-Zip\7z" -sdel a -mmt -mx3 -tzip "%BakDirectory%!LastDate!_Logs.zip" "@%BakDirectory%\ Filename_Value "
【问题讨论】:
标签: batch-file zip task 7zip rar