7-Zip Batch File: Maximum Compression and the Date in the Archive Name
I’m using the 7-Zip archiver more and more. What draws me to it is the excellent compression (sometimes much better than RAR). It does work a bit slower and needs more memory, but for overnight backups that hardly matters. And it has one more big advantage – it’s free!
Below is the batch file I use every night to pack my work folder with maximum compression and the date in the archive name, what each switch does, and a couple of tricks readers asked me about in the comments.
Why 7-Zip beats plain ZIP
- Compression. Its own 7z format uses the LZMA method and packs files as one “solid” stream, so the archive is usually noticeably smaller than a ZIP, especially for documents, text and source code. With files that are already compressed (JPG, MP3, video), no archiver will work miracles.
- Free and open source. Most of the code is under the GNU LGPL, so you can use it at home and at work without buying licenses.
- Reads other formats. It packs to 7z, ZIP, GZIP, BZIP2 and TAR, and also unpacks RAR, CAB, ISO, ARJ and much more – no separate RAR extractor needed.
- AES-256 encryption. You can protect an archive with a password, and in the 7z format even hide the file names.
There’s one downside: not everyone can open 7z. If you’re sending an archive to someone who may not have 7-Zip, pack it as a regular ZIP – 7-Zip can do that too.
Batch file: a nightly archive with the date in the name
My version packs the e:\work folder, with all its subfolders, into f:\arch:
@echo off set Z=d:\exe\7z.exe set DST=f:\arch\work_%date%.7z "%Z%" a -t7z "%DST%" "e:\work\*.*" -r ^ -ssw -ssc -mx9 -mmt
The paths d:\exe, e:\work and f:\arch are mine – change them to yours. If 7-Zip is installed the usual way, 7z.exe lives in C:\Program Files\7-Zip. The quotes around paths are needed if they contain spaces, and the ^ at the end of a line simply continues the command on the next line.
What the switches do:
a– add files to an archive (if the archive doesn’t exist, it is created);-t7z– archive type 7z;-r– include files from all subfolders;-ssw– also pack files that are currently open in other programs (without this switch, 7-Zip skips them with a warning);-ssc– case-sensitive matching of file names (on Windows it usually makes no difference);-mx9– maximum compression level, “Ultra”;-mmt– compress in several threads, which is faster on a multi-core CPU.
There’s a catch with -ssw: if a program is writing to a file at that very moment (a database, for example), the archive may get an inconsistent copy of it. It’s better to dump databases separately with the database’s own tools and put the dump into the archive.
I run the batch file at night via Windows “Scheduled Tasks” (in Vista it’s “Task Scheduler”), and in the morning a fresh archive is already waiting on another drive.
If the date comes out with slashes
%date% inserts the date in whatever format is set in the Windows regional settings. With a day.month.year format (Russian or German Windows, for example) it’s 14.08.2008, the name becomes work_14.08.2008.7z, and all is well. But US-English Windows gives 08/14/2008 (sometimes with the day of the week in front), and the slashes in the file name break the command.
It’s more reliable to get the date from PowerShell: the format then doesn’t depend on the settings, and archives dated like 2008-08-14 also sort by name in strict order. Here is the same batch file in that version:
@echo off
set PS=powershell -NoProfile -Command
set F="Get-Date -Format yyyy-MM-dd"
for /f %%d in ('%PS% %F%') do set D=%%d
set Z=d:\exe\7z.exe
set DST=f:\arch\work_%D%.7z
"%Z%" a -t7z "%DST%" "e:\work\*.*" -r ^
-ssw -ssc -mx9 -mmt
If PowerShell isn’t on your system yet, you can download it free from Microsoft’s website.
A dated archive from the “Send to” menu
Readers asked whether you can pack with the date right from the right-click menu, via “7-Zip → Add to archive…”. In that dialog you only need to choose compression and other settings once – they are remembered – but it can’t put the date into the name. The workaround is your own batch file in the “Send to” menu.
Save a file in Notepad, say 7z-date.bat, with this text (fix the path to 7z.exe if yours is different):
@echo off
set Z=C:\Program Files\7-Zip\7z.exe
set PS=powershell -NoProfile -Command
set F="Get-Date -Format yyyy-MM-dd"
for /f %%d in ('%PS% %F%') do set D=%%d
"%Z%" a -t7z "%~dpn1_%D%.7z" "%~1" ^
-ssw -mx9 -mmt
- Press Win+R, type
shell:sendtoand put this file, or a shortcut to it, into the folder that opens. - Now right-click a file or folder → “Send to” →
7z-date.bat, and an archive likeReport_2008-08-14.7zappears next to it.
Select one file or folder at a time: the batch file packs only the first selected item.
More useful switches
-pPassword– encrypt the archive with a password (AES-256); add-mhe=onand, in the 7z format, even the file names stay hidden without the password;-v100m– split the archive into 100 MB volumes, for example to burn them to discs or send in parts;-sfx– create a self-extracting exe archive that opens even where 7-Zip isn’t installed.
The password sits in the batch file as plain text, so don’t leave such a file lying around.
7-Zip without installing
You don’t have to install the archiver: copying just 7z.exe and the 7z.dll library is enough – handy for a USB stick or a server. There is also a separate console version, 7za.exe, which doesn’t need the library at all.
As of November 2026
7-Zip is still actively developed: the latest version is 26.06, released on November 6, 2026. In 2025–2026 several vulnerabilities related to extracting specially crafted archives were fixed, so an old version is worth updating. Download the program only from the official site, 7-zip.org. The batch files above also work on Windows 10 and 11, where “Scheduled Tasks” is called “Task Scheduler”.
An archive on the same computer won’t save you from theft, fire or ransomware, so it’s wise to keep one copy of important archives off-site, for example in pCloud cloud storage (affiliate link).
P.S. Big respect to Igor Pavlov, the author of 7-Zip!
