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

36 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

    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.

    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

  20. Punit on

    thanks… it really worked..

  21. Peter on

    I have over a million of files (about 200 GB of data) and I would like to back them up to the cloud on a daily basis. The fact that there are so many files it makes it impossible and I was looking for a solution where I compress each folder to an individual file before uploading. I found this script that I use it successfully to do so. But there are 2 considerations.

    Update the archive ONLY when there are file changes inside the directory (new files, updated files, and deleted files)

    1. I would like the output file NOT to change date of creation if there are no file changes inside the directory.

    Here is the script I used but it needs some expanding based in what is the objective.

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

    I appreciate any help on that…


    • James McCarthy on

      Hi Peter. I think your requirements might require a more complicated script or an alternative like rsync (or whatever a Windows equivalent of that).

      Perhaps something easier to set-up like https://freefilesync.org/ might be an option?


    thank ou this helped much

    • James McCarthy on

      You’re welcome!

  23. Mike on

    Hey, James. Hopefully, you are still replying to messages here. I was wondering if there was a way to 7z all contents of each folder into an archive without including the folder itself.

    For example, I have temp1, temp2, temp3 etc folders each with files in it. I want the batch to 7z the contents of each folder without including the “temp1” etc folders in the archive. Is this possible?

    • Mike on

      I should specify I want the contents of each folder zipped individually into separate archives just like your original script only without the folder included in each archive.

      • James McCarthy on

        Hi Mike. I believe you would just add an asterisk in a key location, so `”%%X\”` becomes `”%%X\*”`. Altogether that would be: `for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a “%%X.7z” “%%X\*” -mx=9 -ms=on -md=26 -mfb=256` — make sure you remove the back ticks, though!

        I am basing this on the answer found here which just pulls from the manual itself! https://superuser.com/a/340062

      • Mike on

        Looking at that site, there are slight differences when excluding the subdir prefix there. It has it like .\subdir\* instead of just subdir\*, so the correct way would be this:

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

        I just tried it and it works! Thanks for the help! You have to write it like .\%%X\* as just putting a * there still includes the subdir.

  24. Jevgeni F on

    Really it is awesome -all about programming, but when you get a situation which you want to solve asap, but it’s complex problem all about a LOT of varieties of options and what can go wrong – mistakes, bugs and headache. At this moment my head start spinning.

    I’ve found accidentally solution on other webpage:
    it’s Total Commander!

    It’s simple and awesome. And by the way, where I can “batch pack files” (read further as “put into archive files and folder in one task” or “mass zipping”) separately (like this in any archive format “abc.txt” into “abc.zip”, folder “docs” into “docs.zip”, or “calc.exe” into “calc.zip”). But better part of Total Commander is option to put one “strong password” for large amount archived files and folders – it’s useful to upload and share some private or not documents.

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 )

Connecting to %s

%d bloggers like this: