Skip to content


Some 7-Zip command line examples

As mentioned in the previous post, 7-Zip is a great piece of open-source compression software. Although you’ll most likely want to use the GUI version for daily use, power users may find the command line version useful for batch jobs. The command line version is bundled in the global installation package, so as far as you installed 7-Zip you do have the command line version installed, too.
Since the 9.x versions have been around for quite a while now and the 4.65 version is getting old, we’ll only cover the 9.x versions (at the time of writing, the 9.15 beta offered as a portable application in the above-mentioned post). If you want to stick with the 4.65 version I suggest you have a look as those examples or at the documentation (the Windows batch commands are of course the same but the 7-Zip parameters are a bit different, and also the executable itself has a different name: 7za.exe instead of 7z.exe).

For all those examples, we’ll assume that 7-Zip is installed in G:\UTILITIES\7-Zip\. Also, note that if you want to launch those commands from within a batch file instead of directly from the command line, you need to add an additional ‘%’ in front of ‘%A’ in the for commands. Finally, we are using LZMA2 with the maximum compression level. If you want LZMA, well, obviously just replace -m9=LZMA2 with -m9=LZMA. It’s actually easy to do a lot more customization, just check out 7-zip.chm in your 7-Zip installation folder.

Batch compressing individual files (non recursive)

This one will compress every file located within the current directory (one archive per file) and which have a bmp or txt extension:
for %A in (*.png *.txt) do "G:\UTILITIES\7-Zip\7z.exe" a -t7z -m9=LZMA2 "%A.7z" "%A"

Batch compressing individual files (recursive)

This one will individually (one archive per file) compress all files located within L:\My\Folder\ (and all sub-folders) and which have a txt or doc extension:
for /R "L:\My\Folder\" %A in (*.txt *.doc) do "G:\UTILITIES\7-Zip\7z.exe" a -t7z -m9=LZMA2 "%A.7z" "%A"
Also, note that all archives will be placed in the same folder as the file they contain (so there will be archives all around all sub-folders).

Batch compressing all folders of the current directory

This one will compress every folder of the current directory (one archive per first sub-level folder):
for /D %A in (*) do "G:\UTILITIES\7-Zip\7z.exe" a -t7z -m9=LZMA2 "%A.7z" "%A"

Source

To write those commands the following pages were useful:

Posted in programming, Windows.


9 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. John Wagner says

    Try

    for /D %%A in (*) do “D:\Program Files\7-Zip\7z.exe” a -tzip -mx=9 “%%A.zip” “%%A”

    You forgot the extra %. I had more comments, but the captcha code ate them.

  2. patheticcockroach says

    Well, for the extra % it depends if you run it from command line or from a batch file, actually 😉 Sorry about the captcha eating your comments… spammers are a nuisance to legit posters 🙁

  3. dev01 says

    I have to use it in SSIS execute process task. How would I write argument there to zip each folder individually in one directory.
    please tell me if some1 know about it.

  4. Davii says

    “… spammers are a nuisance to legit posters”
    Granted, but so is badly written captcha code 😉

  5. patheticcockroach says

    Well, it’s not my code 😛 More seriously, this captcha is actually quite easy to read. I actually chose it because of that: it was both “uncracked” yet easy to read. Now it’s cracked, though 🙁
    Also normally a failed captcha doesn’t lose your comment… as long as your browser doesn’t have a messed up history back function.
    On a side note, the e-mail field is optional in the comments 😉 (although it doesn’t seem so because of the misdesigned skin :/)

  6. Daky says

    Thank you all, that was very useful 😀

  7. ticotexas says

    +1
    excellent, just what i needed

  8. Gwx says

    Thank you.

  9. Brian says

    Does anyone know how to convert this to a bash (shell script) to run on linux?

    for /D %A in (*) do “G:\UTILITIES\7-Zip\7z.exe” a -t7z -m9=LZMA2 “%A.7z” “%A”

    I don’t need the compression part from (-m9 to LZMA2 in the above command) I just need to know the conversion from batch to bash (shell script)

    Batch compressing all folders of the current directory



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA