This guide covers the use of the WinRAR command-line tools to compress and uncompress files in a directory, and their use in batch files. This guide is an extension of a previous post, Automate Zipping Tasks using the Command-line Interface, that covered the use of command-line tools for two free compression utilities, IZArc and 7-Zip. The information in this guide was tested on a Windows PC running Vista.

compress

WinRAR is a popular and powerful archive manager that includes the command-line tools rar.exe and unrar.exe. Rar.exe is used to compress and unrar.exe to uncompress. Both executables are located in Program Files\WinRAR in the installable version. Although WinRAR is shareware, it can be used on a trial basis for 40 days. Using WinRAR’s command-line tools is similar those for IZArc and 7-Zip. The syntax for the executables is:

WinRAR <command> -<switch1> -<switchN> <archive> <files...> <@listfiles...> <path_to_extract\>

Examples to compress a folder:

rar a -r yourfiles.rar *.txt c:\yourfolder

creates archive yourfiles.rar and compresses all .txt files in c:\yourfolder and subfolders

rar a yourfiles 

creates archive yourfiles.rar and compresses all files in the current folder, but doesn’t include subfolders (note lack of extension, WinRAR will use the default extension .rar)

“a” command adds to archive

“-r”  switch recurses subfolders

Examples to uncompress a folder: 

unrar x c:\yourfile.rar *.gif c:\extractfolder\

extracts all *.gif files from yourfile.rar to c:\extractfolder\ (trailing backslash required) and restores the folder structure

unrar e c:\yourfile.rar 

extracts all files in c:\yourfile.rar to the current folder (folder structure ignored)

“x” command extracts with full paths

“e” command extracts and ignores paths

Basic rules for WinRAR:

  • When files or listfiles are not specified, all files in the current folder are processed
  • When specifying all files in a folder, yourfolder or yourfolder\*.* are equivalent
  • Folder structures are automatically saved in archives
  • WinRAR  uses the default .rar extension, but that can be overridden by specifying the zip extension in the archive name
  • Switches and commands are not case sensitive and can be written in either upper or lower case

Another point is that WinRAR doesn’t appear to use the Windows path environment variable, so it must be specified either at a command prompt, set permanently in the environment variable settings, or specified in a batch file.

To set it temporarily at a command prompt or in a batch file, just enter

set path="C:\Program Files\WinRAR\";%path%

To set it permanently in the Windows path, use start–>Control Panel–>System–>Advanced system settings–>Advanced Tab–>Environment Variables–>System Variables–>Path–>Edit. Add the path ;C:\Program Files\WinRAR; to the end (don’t forget the single semicolons at the beginning and end). Hit OK three times.

Using WinRAR in Batch Files:

Two batch file examples are provided. The first compresses all files in a folder, with the option to compress all files into a single archive or to compress them individually. The second batch file decompresses all .rar files from a folder and places the extracted files into another directory. Be sure to change the extension(s) to .bat before using either file.

compress_rar.bat

uncompress_rar.bat

 

Share

This guide demonstrates how to automate repetitive compression tasks using the CLI (command-line interface) or batch files with two popular free archiving tools, the command-line utility for IZArc (freeware) and the command-line version of 7-Zip (open-source). Batch files are used because they are fairly easy to create, popular and can operate on several versions of Windows and DOS. Although batch files (*.bat) are covered in this guide, other scripting languages may also be used.

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 sought out 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 have a CLI built-in or available as a separate add-on (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)

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

For 7-Zip:
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.

For IZArc:
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:

For 7-Zip:
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):

For IZArc:
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 uses batch file examples compresses all files from a folder into a single zip archive with a unique date suffix, moves the zip archive to another folder, and then deletes the original files. The thumbnails above show the batch files for 7-Zip (bottom left) and IZArc (bottom right). Click to enlarge them to see the contents. In the 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.

An explanation of the 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. "%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. In this case, it’s all the files in the folder “testzip” (this includes any subfolders regardless of the “-r” switch setting, see use below).
  • “-mx5″ = (optional) switch that sets the 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.

An explanation of the 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. "%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.

Summary

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


Share

Bad Behavior has blocked 373 access attempts in the last 7 days.