您能否建议我如何在其子文件夹中的文件夹下压缩文件,并使用批处理脚本将文件重命名为创建日期,然后删除原始文件。它应该能够使存档的天数可配置。例如,如果我想归档任何超过7天的文件,它应该压缩超过7天的文件。并且还可以配置删除文件。
例如,文件是在2016年9月14日创建的abc.log,它将是zip并重命名为abc.20160914.zip
我通过论坛搜索但未找到。非常感谢您的帮助。
答案 0 :(得分:1)
我找到了答案
@echo off
setlocal enabledelayedexpansion
set Archive=D:\scripts\test\Archive
set Temp=D:\scripts\test\Temp
set DaytoDelete=7
REM Moving file older than %DaytoDelete% days to TEMP folder
for /f "tokens=*" %%G in (D:\scripts\test\Location.txt) do (
forfiles -p "%%G" -s -m *.log -d -%DaytoDelete% -c "cmd /c move @path %TEMP%"
)
if not exist "%Archive%" (md "%Archive%")
if not exist "%Temp%" (md "%Temp%")
REM Zip file in %Temp% and rename zip file with creation date
for %%a in ("%Temp%\*.log") do (
echo Processing %%~nxa ...
set File=%%~fa
set Archive=D:\scripts\test\Archive
for /f "tokens=1* delims=," %%a in ('wmic datafile where "name='!File:\=\\!'" get 'CreationDate' /format:csv ^| find /i "%ComputerName%"') do (set CreationDate=%%b)
echo %%~nxa: !CreationDate!
set cYear=!CreationDate:~0,4!
set cMonth=!CreationDate:~4,2!
set cDay=!CreationDate:~6,2!
set FileTime=!cYear!!cMonth!!cDay!
REM zip "%Archive%\temp.zip" %%~fa Issue with zip command, it zips whole full path of file
"C:\Program Files\7-Zip\7z.exe" a -tzip "%Archive%\temp.zip" %%~fa
ren %Archive%\temp.zip %%~na.!FileTime!.zip
)
REM Delete file after zipping
DEL /F /S /Q %Temp%\*.*
pause