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 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)

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 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

Added 03/05/2013

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:

For 7-Zip:

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).

For IZArc

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:

zip_listfile.bat

mylistfile.txt

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

7 Comments

  1. Ramniwas says:

    your artical for 7Za batch is verry helpful for me Thanx of lot it is done after 5month again thank you

  2. admin_tb says:

    Try this. It’s not 100% tested. You may want to try it out with some test files first. Just enter your file paths in the variables dirA, dirB, dirC. I didn’t see anything in your request about zipping files with WinRAR; just file manipulation.


    REM checkmovewav.bat 1/3/12

    :start

    @echo off
    setlocal

    :init

    set dirA=C:\Utilities\Batchfiles\wavinputfolder\
    set dirB=C:\Utilities\Batchfiles\winrarzipfolder\
    set dirC=C:\Utilities\Batchfiles\winrarzipsnotfound\

    if not exist "%dirA%" echo "%dirA%" not found! & goto :EOF
    if not exist "%dirB%" echo "%dirB%" not found! & goto :EOF
    if not exist "%dirC%" echo "%dirB%" not found! & goto :EOF

    for /R "%dirA%" %%f in (*.wav) do (
    if not exist "%dirB%%%~nf.zip" (
    echo "%%~nxf" NOT found in "%dirB%"
    copy "%%f" "%dirC%"

    ) else (

    echo zipped version of "%%~nxf" FOUND in "%dirB%"
    del "%dirB%%%~nf.zip"
    move "%%f" "%dirB%"

    )
    )

    echo.
    echo Operation Completed
    echo.

    pause

    :eof

  3. Can anyone write me a script to do the following?
    Write a script to make WinRAR do the following please?

    > 1.. To read a directory called H:\wavinputfolder sequentially;
    > 2.. To extract the file name (not extension);
    > 3.. To look up a file (not extension) corresponding to this name in
    > H:\winrarzipfolder;
    > 4.. If not found, to copy the file (and its .wav extension) from
    > H:\waveinputfolder to H:\winrarzipsnotfound and then loop back to 1 to read
    > the next wav; at end to exit;
    > 5.. If found, to delete the contents of the zip file and replace it with
    > the wav file (complete with its extension).
    > 6.. To loop back to 1 until at end exit.
    > Example:

    > H:wavinputfolder H:\winrarzipfolder
    > H:\winrarzipsnotfound Action

    > Fred.wav Fred.zip
    > Replace contants of fred.zip, viz., with fred.wav
    > Sue.wav Sue.zip
    > As above
    > Mike.wav Joe.zip
    > Mike.wav Copy (not cut) mike.wav to
    > the “notfound folder”
    > Joe.wav Joe.zip
    > As for fred and sue
    > End of folder
    > Exit

    > Thank you.

    > Keith J.Salmon

  4. Pablo says:

    can anybody please help, I need to set up a watched folder on a windows 2003 server that will .zip whatever is dropped into it wether it be a loose file or a folder, it could be via a third party app or scripting. Any info will be much appreciated

  5. You Can Automate Repetitive File Management has software to automate zipping by recording a script in Windows Explorer. The software creates a VBScript which executes 7z to do the zipping.

  6. Elron FoI says:

    The following sample shows how to use the IZArcc.exe with files listed and using a file containing a list of files and directories to be compressed. It also shows how check for USB drives and if they’re not there to use the work directory as the backup target.

    The IZAacc.exe commands use switches that are explained in the manual.txt. Not explained in the manual is the format of the @filename.txt list file. The format is one path\file(s) per line, no quotes needed. The semicolon is needed only if two entries are on the same line. If the Drive letter is missing the current drive is used (as in the first line of the sample):

    —————–Sample format: filename.txt————–
    \path\a directory\file
    C:\path\a directory\file
    D:\path\a directory\file
    ———————— End sample

    Besides showing the IZArcc.exe commands, this script also shows checking for two backup locations that may not be available. In my case they are USB attached storage devices, however they could be network drives, HDDs, or whatever. In the code the drive letters are known, however if this is not the case, one has the option of entering the letters as parameters. The code uses the code’s home directory as a default, but switches to one of the USB drives if either is attached. At the end if both drives are attached the backup file is copied to the second drive. Note that this code depends on the order in which the drives are checked.
    Why? I’ve learned to like redundant backups.

    The last line “exit /B” is suppose to end the code but leave the cmd window open so that the process can be manually checked (This is why I don’t check for %errorcode%). however, the cmd window doesn’t stay open if the file is run by double clicking on the filename. cmd has to be opened from Start->run… and the file started at the prompt.
    (Painfull, but what do you expect from MicroSlop?)

    Notes:
    + If you cut & paste this; check for lines of code that have been split into two lines.
    + Spacing on a couple of sets of lines is done so that exact same parts of two or more lines align vertically, in notepad (using non-proportional font).
    + When changing to your own directorys\files:
    Watch the use of quotes (“) MicroSlop is a pain about these.
    Watch out for …%\%… vs …%%…
    + Yes there are other ways of writting this, but this code is running on my XP SP2 computer which is better than somebody’s theory on how it should be written.

    Finally, I don’t hang out here. I just put this here because this page was the only one I could find with any decent info on IZArcc.exe scripting. (i.e., don’t email me, I’ll email you).

    ——-Start code———

    @echo off
    REM 10-Apr-04 Written – backup files to USB drives or backup dir ; ElronFoI

    REM set MYDAY to sort in date order by filename (yy-mm-dd…)
    set MYDAY=%date:~12,2%-%date:~4,2%-%date:~7,2%

    REM note that %cd% is used in commands
    P:
    cd “\P My Docs\Backup”

    Rem Default to save in backup dir; but If either USB drive pluged, in save it there
    set MYBAK=%cd%
    set MYTMP=\Photos\New Folder\New Folder\trip pictures\PMyDocs old
    if exist “i:%MYTMP%” ( set MYBAK=i:%MYTMP%
    ) else ( if exist “f:%MYTMP%” set MYBAK=f:%MYTMP% )
    set MYBAK=%MYBAK%\%MYDAY% My Pictures.zip

    “c:\program files\izarc\IZArcc.exe” -a -P “%MYBAK%” “\P My Docs\-*.*”
    “c:\program files\izarc\IZArcc.exe” -a -P -r -w “%MYBAK%” @”%cd%\backup-list.txt”

    REM If both USB drives (i:, f:) plugged in, copy from i: to f:
    if exist “i:%MYTMP%” (
    if exist “f:%MYTEMP%” copy /V /Y “%MYBAK%” “f:%MYTMP%\%MYDAY% My Pictures.zip”
    )

    Set MYDAY=
    set MYBAK=
    set MYTMP=

    END
    exit /B

    ——-End code———

Bear

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