You may be thinking, “why would anyone want to automate a compression task in the first place”? Previously, I had a similar attitude until I had a need to compress, copy, rename, and move files from the same folder regularly, so I looked for a way to automate this process to save time and reduce errors.
This guide covers the use of two free archiving utilities, the command-line add-on utility for IZArc and the command-line version for 7-Zip. These two utilities were chosen because they are free, work well, and include a CLI executable or have it available as a separate add-on (as for IZArc). Using either of these utilities to automate your archiving needs is fairly easy to do, costs nothing, and saves time. Although this guide is designed to enable the user to accomplish this task quickly and easily, it is assumed that the reader is already familiar with creating and using batch files and has some experience with using compression utilities. WinXP was used to verify the information in this guide.
Step 1. Download one of the following (32 bit versions were used for this guide)
- Get the 7-Zip Command-line Version from the 7-Zip download page
- Get the IZArc utility and the IZArc Command-line Add-On from the IZArc download page
The versions used for this guide were: 7-Zip Command Line Version 4.65, IZArc Version 4.1, and IZArc Command line Add-On Version 1.1.
Note: This guide uses the Command Line Version of 7-Zip (7za.exe), which is a stand-alone version of 7-Zip that supports only the 7z, cab, zip, gzip, bzip2, Z and the tar formats. The full version of 7-Zip (installable and portable versions) contains a command line executable (7z.exe) that is more full-featured and explained in the 7-Zip help files. The IZArc command-line utility is an add-on to IZArc and supports the zip, jar, bh, cab, and the lha formats.
Step 2. Install the utility and set the path variable
Extract the 7-Zip file to a location of your choice, for example C:\7zip. For convenience and to make the 7-Zip commands available to the batch file, manually set the path variable for the chosen location, e.g., C:\7zip. To set the path, right click My Computer–>Properties–>Advanced Tab–>Environment Variables. In the System Variables Area, select path, then click the Edit button. Add “;C:\7zip;” (or your folder location) to the end of the line (don’t include the quotes and don’t forget the semicolon at the start and end). Hit OK three times.
First, install the IZArc utility and then install the IZArc Command-line Add-on to their default locations (C:\Program Files\IZArc) by double clicking them. The IZArc Command-line Add-on will install the Command-line executables izarcc and izarce to the IZArc directory in C:\Program Files\IZArc. The Path Variable is not set automatically during the installation, so it must be done manually. To set the Path, right click My Computer–>Properties–>Advanced Tab–>Environment Variables. In the System Variables Area, select path, then click the Edit button. Add “;C:\Program Files\IZArc” to the end of the line (don’t include the quotes but don’t leave out the semicolon at the start). Hit OK three times.
Step 3. Test the installation
Open a CMD window by hitting the Start button, enter “cmd” and press “OK”. A command-line window will open. Type one of the following into the command prompt window:
Type “7za” and then press ENTER. If installed correctly, you should see the usage commands for 7-zip as shown in the top left thumbnail (click the picture to enlarge):
Type “izarcc” and then press ENTER. If installed correctly, you should see the usage commands for IZArc as shown in the top right thumbnail (click the pictures to enlarge and view content):
Note: For IZArc, the command-line executables are izarcc for the compression function and izarce for the extraction function. If you didn’t get one of the screens shown above, then check that the Path Variables are present and correct for the 7-Zip or IZArc installation.
Step 4. Create the batch file
There are many ways to create batch files: from the simple to the complex. For the sake of simplicity and to focus on quickly demonstrating the use of these two compression utilities, this guide includes two example batch files (one for IZArc and another for 7Zip) that will compress all files from a folder into a single zip archive with a unique date suffix, move the zip archive to another folder, and then delete the original files. The thumbnails above show the contents of the batch files for 7-Zip (bottom left) and IZArc (bottom right). Click to enlarge them to view the contents. For both batch files, the folder “testzip” contains the files to zip and the folder “testmove” is where the archive is to be moved.
Depending on which utility you wish to use, click one of the following links to open the batch file text in a new window where you can cut and paste it into your text editor or download it: testizarc.txt (IZArc) or test7zip.txt (7-Zip). Note: if you use either one of these batch files be sure to change the extension to .bat, create the “testzip” and “testmove” folders on your C: drive, and copy some files you want to zip to the “testzip” folder. Of course you can change either batch file for your own needs using a text editor to change the zip folder (testzip) and move folder (testmove) to folders of your choice.
7za command-line in the 7-Zip batch file (test7zip.txt):
7za a -tzip "C:\testmove\xxxx_%TODAY%.zip" "C:\testzip\*.*" -mx5
- 7za – (required) this starts the 7-Zip command-line executable.
- a – (required) command to add files to the archive.
- -tzip – (optional) switch to set the type of archive; in this case, it’s a zip file (optional unless using another compression format).
- C:\testmove\xxxx_%TODAY%.zip – (required) the name of the archive to create.["testmove" is the folder where the archive is to be created. "xxxx" is the name of the archive (replace "xxxx" with desired name). "%TODAY%" is today's date and time variable added to the zip archive name to insure a unique archive name is created each time the batch file is executed. The parentheses are optional unless there are spaces in the file path. Finally, ".zip" is the type of archive to be created.]
- C:\testzip\*.* – (required) the files to be archived. All the files with an extension in the folder “testzip” (this includes any subfolders regardless of the “-r” switch setting, see use below).
- -mx5 – (optional) switch to set compression method. In this case, it’s zip mode, compression level 5 (level 5 is normal compression which is the default level).
When using the zip format and normal compression, the optional switches can be eliminated and the command shortened as shown below:
7za a "C:\testmove\xxxx_%TODAY%.zip" "C:\testzip\*.*"
Concerning subfolders: Officially, the “r” switch is used for searching and unless included, recurse subdirectories is disabled. However, because of a glich in the program, the zip command always includes subfolders regardless of “r” switch setting in situations where all files in a folder are to be archived using the wildcards “*.*”. For this reason, the “r” switch is not used in any of the 7-Zip example commands in this guide. See 7-Zip forum topic or 7-Zip FAQ’s – Why doesn’t -r switch work as expected? for further information.
IZArc command-line in the IZArc batch file (testizarc.txt):
izarcc -a -cx "C\:testmove\xxxx_%TODAY%.zip" "C:\testzip\*.*"
- izarcc – starts the izarcc compression executable.
- -a – adds files to the archive.
- -cx – sets the compression level to maximum.
- C:\testmove\xxxx_%TODAY%.zip – the name of the archive to create.["testmove" is the folder where the archive is to be created. "xxxx" is the name of the archive (replace "xxxx" with desired name).%TODAY% is today's date and time variable added to the zip archive name to insure that a unique archive name is created each time the batch file is executed. The parentheses are optional unless there are spaces in the file path. Finally, ".zip" is the type of archive to be created.]
- C:\testzip\*.* – the files to be archived. In this case, it’s all the files in the folder ” testzip” (this doesn’t include any subfolders unless the “-r” command is added, see usage below).
Additionally the izarcc switches “-r” (include subfolders) and “-p” (store relative pathnames) may be used:
izarcc -a -r -p -cx "C:\testmove\xxxx_%TODAY%.zip" "C:\testzip\*.*"
-r – recurse into subfolders (include subfolders)
-p – store relative pathnames (maintains directory structure)
Currently, the IZArc help file for the IZArc program, IZArc.chm, contains limited information for the izarcc (compress) or izarce (extract) command-line functions; however, the command-line Add-on utility does include a text manual (manual.txt) that explains the commands and includes some examples for their use.
Using List files
List files are files that contain a list of one or more files and/or directories separated by new line symbols. Both the 7-Zip and IZArc command line utilities can use list files for processing files and directories. 7-Zip uses UTF-8 encoding for list files by default, but the encoding can be changed at the command line using the “-scs” switch if necessary (see the command line version section in 7-zip.chm for further information).
The command line syntax for compressing files and/or directories with a list file:
7za a -tzip "C:\Your Folder\yourarchivename.zip @"C:\Your Folder\yourlistfile.txt"
- 7za – the 7-Zip command-line executable.
- a – add files to the archive command.
- -tzip – set archive type to a zip file (optional unless using another compression format).
izarcc -a -r -p -cx "C:\Your Folder\yourarchivename.zip @"C:\Your Folder\yourlistfile.txt"
- izarcc –starts the izarcc compression executable.
- -a – adds files to the archive.
- -cx – sets the compression level to maximum.
- -r – recurse into subfolders (include subfolders)
- -p – store relative pathnames (maintains directory structure)
Examples for a list file and a batch file that illustrate using a list file with the 7-Zip and IZArc command line utilities are below. The batch file contains commands for both utilities using a single list file. Be sure to change the file and path names as appropriate for your situation and the batch file extension to bat before using:
It’s fairly easy to automate archiving tasks using a simple batch file using either of the two free utilities covered in this guide. For basic needs, the command-line utility for IZArc or the 7-Zip command-line version (7za.exe) should suffice. For more complex requirements, the full version of 7-Zip (7z.exe) contains additional features; allowing users to perform more sophisticated archiving tasks from the command-line.
For more information and examples for using the 7-Zip command-line, also see the following:
Dot Net Perl’s article, 7-Zip Command-line examples
Codejacked’s article, ZIP Up Files from the Command Line
The official 7-Zip Manual, 7ZIP manual & documentation