Batch file with date and time for zipping

May 25th, 2025 – 10:07 am
Categorized as Computing Notes

WinRAR offers great feature. It will create a zip file using a name mask.  That allows one to create a file with an excellent name via the right click menu.  Buying many WinRAR licenses becomes cost prohibitive.  This batch file will do the same thing using 7Zip on Windows. The batch file must be saved in the SendTo folder on Windows.  7Zip must be installed

@echo off
REM create a timestamped zip file of a directory
REM ^ is a line continuation mark
FOR /F "TOKENS=1* DELIMS= " %%A IN (^
'DATE /T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN (^
'DATE /T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN (^
'echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN (^
'echo %CDATE%') DO SET yyyy=%%B
for /f "tokens=1-3 delims=:." %%A in ("%time%") do (
    set hours=%%A
    set minutes=%%B
    set seconds=%%C)

SET date2=%yyyy%-%mm%-%dd%_%hours%%minutes%
"C:\Program Files\7-Zip\7z.exe" a -tzip^
 C:\archives\%~n1_%date2%.zip %1
pause

This will save the zip filed in C:\archives\zips with a filename that consists of the original directory name and a timestamp of the form directoryname_2022-01-28_2212.zip. To use it, right click a directory, and choose SendTo –> TheBatchFileName. In my case, the file is named 7zipBackup.bat