2016-09-14 105 views
-1

您能否建議我如何在其子文件夾中的文件夾下壓縮文件,並使用批處理腳本將文件重命名爲創建日期,然後刪除原始文件。它應該能夠將存檔的天數設置爲可配置。例如,如果我想存檔任何超過7天的文件,它應該壓縮超過7天的文件。同時也使刪除文件可配置。帶有創建日期的zip文件

例如,在2016年9月14日被abc.log創建的文件將是拉鍊,重命名爲abc.20160914.zip

我通過論壇,但沒有發現搜索。非常感謝您的幫助。

+0

你好迪恩生物的答案,請您可以張貼一些你已經嘗試過或望着批次代碼。 – Jonas

+0

你使用Winrar,7zip等...? – Jonas

+0

Check out http://stackoverflow.com/questions/30079757/rename-file-with-creation-date-time-in-windows-batch – Jonas

回答

0

我找到這個

@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