7-Zipping multiple folders to separate/individual zip/archive files

So I thought I would write this solution up as I had already made a batch file to do this then promptly deleted it (subsequently Recuva couldn’t ‘recuva’ it, as I’d been throwing a lot of data on and off my PC and thus I assume it was over-written).  The problem was I had several folders I wanted to zip into individual 7z archives.  7-zip lacks a ‘Add to “*.7z”‘ explorer context for multiple selected files or folders (even though it does include a ‘Extract to “*\”‘).  Nor does this option appear within the application itself or via the command-line.  I seem to recall this feature being available in the Explorer contexts for some ZIP program I once used (I think it was TUGzip), but I wanted to choose quite aggressive compression settings for the archives and I didn’t think this would be achievable except via the command-line.

So using a bit of Google skills I discovered the ‘for… in… do’ batch command.  Which is quite powerful and I’m sure will come in handy again now that I know about it.  For more information check out this guide: More Powerful Batch Files Part II – Iterating with “For”.  Coupled with the 7-zip manual I arrived at a batch file with this line in it:
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\" -mx=9 -ms=on -md=26 -mfb=256
So copy that into a batch file, copy it to the directory where you have folders you want to zip to individual archives and run it.  You might want to change (or just delete) the custom settings (e.g. the settings with -m at the end) as it set up to do some pretty heavy compression which does take quite a long time (but of course you can leave it running in the background without too much problem).

About these ads

26 comments so far

  1. Andy on

    thanks, worked a treat!

  2. TheMystical on

    Awesome, what I was looking for, such a shame 7-z didn’t integrate this in the context menu. You saved me a lot of time, thanks

  3. pepito on

    Thanks!
    you just saved me a couple of hours of finding out how to write that little script.

  4. EnripsSeepers on

    Looks like you are a true pro. Did you study about the theme? haha..

  5. Hazuki Amamiya on

    In case there is error while running the command, try to eliminate a “%” and the command may work for you again.

    e.g.
    for /d %X in (*) do “c:\Program Files\7-Zip\7z.exe” a “%X.7z” “%X\” -mx=9 -ms=on -md=26 -mfb=256

  6. Webkido13 on

    Great! Worked like a charm. Saved me hours of work. Thanks!

  7. Phil on

    Thanks !
    It worked like a charm ;)

  8. Jimi Lee on

    Thanks a lot!!

  9. Zephyros on

    Thanks, I was searching for something similar to work with multiple files instead of directories, just had to modify your script a bit and it works great!

    for %%X in (*.pdf) do “C:\Program files\7-Zip\7z.exe” a “%%X.7z” “%%X” -mx=9

    Thanks again!

  10. theclubpenguineherald on

    Thanks a lot, works like a charm.
    By the way, I modified it this way
    for /d %%X in (*) do “C:\Program Files\7-Zip\7z.exe” a “%%X.zip” “.\%%X\*”

    * It creates zip instead of 7z
    * Contents of each folder is compressed instead of compressing the folder itself.

  11. Hannelore on

    Works like a charm. Thank you.

  12. kyle b on

    Ok so what corrections would I need to do to this scrip to have it compress using LZMA2 Dictionary Size=128MB, Word Size=273?

    Thanks in advance for this help :) this scrip did save me a lot of time and effort.

  13. kyle b on

    I’m sorry me again :( what would I need to add to have the files automatically split say like every 50mb?

  14. wfz on

    I need to compress all the files in a directory into its own 7z file. I tried
    for %%X in (*.*) do “C:\Program files\7-Zip\7z.exe” a “%%X.7z” “%%X” -mx=9,
    but it did nothing. The folder way works great!

    Any other way to do what I need?

    • James on

      Use this instead:

      for /f “tokens=*” %%G in (‘dir /A:-D /b’) do “c:\Program Files\7-Zip\7z.exe” a “%%G.7z” “%%G” -mx=9 -ms=on -md=26 -mfb=256

      The “/A:-D” means directories are not included. The “tokens=*” means long names don’t break the script.

  15. wfz on

    thanks, that works well!

  16. Carlos on

    I want to create a ZIP files instead of .7zip so i added the -tzip into the script but it doesnt work, any ideas?

    for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a -tzip “%%X.zip” “%%X\” -mx=9 -ms=on -md=26 -mfb=256

    That is what i used.

    • James on

      Maybe try without the extra switches, i.e. try without the “-mx=9 -ms=on -md=26 -mfb=256″.

  17. Joe Shabadu on

    Works great for me, thank you very much! In case anyone is interested, here is the one I am using, which does zero compression and uses a zip file (so it can be opened in Windows Explorer), and includes the contents of the folder rather than the folder itself (thank you theclubpenguineherald!)

    for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a -tzip “%%X.zip” “.\%%X\*” -mx0

    • Ridwan on

      Hi, already copied it to my batch file, but it doesn’t work. do you have any clue?
      i just copy-paste your code to my file and the command prompt window only flashes..
      for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a -tzip “%%X.zip” “.\%%X\*” -mx0

      please help, i need to compress a whole lot of images stored in multiple (and a lot) of folders, to make it easy when i need to move them around..

      • MACC on

        I tried it as well and it didn’t work. There’s something wrong with that script.

      • MACC on

        The correct script to compress every folder into individual zips is:

        for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a “%%X.zip” “%%X\”

        Hope this helps!

  18. Brad on

    Once I’ve created multiple individual .zip files in a folder, is there a way to batch process these same files to be .exe files that when executed will be placed to the same come file folder location? Any help would be appreciated. Thanks

    • James on

      Hi! Do you mean you want to combine all of the 7z files into an exe file? You could include something like this in your script:

      “c:\Program Files\7-Zip\7z.exe” a -sfx files.exe *.zip

  19. techfranz on

    This is a great article! One other thing to note is if you want to store the batch file in a folder with all your other batch files 7zip let’s you change the directory your are archiving the batch command “cd”. Also you can put your 7zip command line utility at a different pat as well.

    cd /D C:\Users\User\Music
    for /d %%X in (*) do C:\Batch\7za920\7za.exe a “%%X.7z” “%%X\” -y -mx9 -mmt -we:\temp


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: