21 Sept 2020

Create automatically updated daily archive using 7-zip and batch

收藏到CSDN网摘

Since lockdown started, I switched to a laptop for my daily work and lost the directly connections to our secured network drive. Although we have alternative secured online space for file transferring, packing and uploading those files frequently is a dumb thing to do from time to time. By adding those tasks to a batch file and pressing the green button seem more sensiable to do then. In this post, a batch script that utilises the free opensource archive file software 7-zip has been shared. Moreover, it will use the current date to name the archive file automatically. Here we go! The following code uses the date only, but it's very easy to expand it to get the current time as well. ( Set _hour=00%%H SET _minute=00%%I) Other formats can be used as long as 7-zip supports. Multiple files/dirs and wildcards are supported straightaway.
@echo off

:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      SET _yyyy=%%L
      SET _mm=00%%J
      SET _dd=00%%G
)
:s_done

:: Pad digits with leading zeros
      SET _mm=%_mm:~-2%
      SET _dd=%_dd:~-2%

:: Display the date/time in ISO 8601 format:
SET _isodate=%_yyyy%_%_mm%_%_dd%

SET fname="ZIP_PATH_AND_NAME.zip"

ECHO zipping file...

"C:\Program Files\7-Zip\7z.exe" a -tzip %fname% "FILE_PATH\*.TXT" "FILE_PATH\*.CPP"

ECHO Done!
GOTO s_end

:s_error
ECHO Cannot find WMIC.EXE 
GOTO s_end

:s_end
exit 0

No comments :

Post a Comment