2014-01-28

寫批次檔利用7zip壓縮log及刪除檔案

寫批次檔利用7zip壓縮log及刪除檔案
以下是我寫的sample



@echo off
echo Juggernaut's auto zip and delete sample
rem SET Date and Time
set ddate=%date:~8,2%
set dmonth=%date:~5,2%
set dyear=%date:~0,4%
set thour=%time:~0,2%
set tminute=%time:~3,2%
set tsecond=%time:~6,2%
set custime=%dyear%%dmonth%%ddate%_%thour%%tminute%%tsecond%
echo %custime%
path C:\Program Files\7-Zip
rem SET benchmark is 10MB
set /a benchmark = 1024*1024*10

cd .\
for %%c in (*.log) do (
rem print related information
echo %%c
echo size : %%~zc
echo fileName : %%~nc%%~xc
echo filepath : %%~fc

rem if file's size > benchmark
IF %%~zc GTR %benchmark% (
rem zip file name
echo zipfileName : .\auto_backup_%custime%_%%~nc.zip
rem source
echo Src : .\%%~nc%%~xc
rem run 7z to zip
rem a : Adds files to archive
rem -tzip : Specifies the type of archive. The format is zip
7z a -tzip .\auto_backup_%custime%_%%~nc.zip .\%%~nc%%~xc
IF NOT ERRORLEVEL 1 (
rem if zip file success
DEL %%~nc%%~xc
rem delete the original file
IF ERRORLEVEL 1 echo Delete %%~nc%%~xc FAIL
)
echo =====================================
)
)
echo Juggernaut's auto zip and delete sample End
:end

參考資料
如何設定批次檔,讓7-zip幫你自動化壓縮兼備份檔案 – 7-zip
【備份/還原】用批次檔定期作備份 (1)
bat 批次檔指令
批次檔計算檔案大小
BAT批次指令: For的功能介紹
批次檔bat最基礎的71個指令

0 意見: