How to 7Zip up a bunch of folders into individual .7z files.

Ever needed to zip up a bunch of folders into individual filenames? Not found it easy in the GUI? This is the solution.

Install 7Zip if it hasn’t been installed already (You can download it here.)

Click for a full size illustration of adding 7Zip to the path
Add 7Zip into your path.

  • Hit Windows Key + I on your keyboard”
  • Search for “environment”
  • Select “Edit the system environment variables”
  • Select “Environment Variables”
  • Select “Path”…
  • Hit “Edit”
  • Hit “New”, and type in c:\Program Files\7-Zip
  • “OK” all the way out

Save the code below into a batch file, e.g. ZipMe.bat. Move the batch file to somewhere in your path

@echo off
for /d %%i in (*) do (
echo processing "%%i"
7z a -mmt12 -mx6 -pSecretPassword -mhe -r "%%i.7z" "%%i"
echo ---------------------------------------------------------------------------------------------------------------------------

Open a CMD prompt, change folder to the level where all the folders you want to zip are present, e.g. if you had a bunch of photo shoots present in your d:\Photo_Shoots folder, and wanted them in individual zip files (You’d saved the batch file as d:\Zipme.bat):

cd \PhotoShoots

An explanation of the batch file

  • for /d %%i in (*) do (stuff happens here)
    • With the For command, everything inbetween the brackets (stuff happens here) is executed for every directory /d in the mask (*)
      So for example, you only wanted it to apply to directories containing the word cat it would be (*cat*) or directories starting with the letter a (a*)
  • The echo command just prints whatever is after it to the window
    • The echo command is quite dumb. If you wanted to print Cat to the window (Technically this is known as the console) it would be echo Cat. To include a variable %%i in this example, it’s just the variable name %%i
  • 7z a -mmt12 -mx6 -pSecretPassword -mhe -r “%%i.7z” “%%i”
    • This is the 7Zip commandline, the payload which does the work:

      7z is the program doing the work

      a is for add, so in the example, you’re adding to a file

      -mmt12 is the number of processor cores to use, 12 in this example

      -mx6 tells 7Zip to use almost maximum compression – see the 7Zip manual for more details

      -pSecretPassword password protects the archive with SecretPassword

      -mhe makes 7Zip encrypt the filenames inside the archive

      -r is recursive – it will go into the sub folders and include them in the zip

      “%%i.7z” is the filename to add to, it’s enclosed in double quotes to ensure that it gets passed to 7Zip as a whole filename, otherwise filenames with spaces wouldn’t work

      “%%i” is the directory to zip up, again in double quotes as it may contain spaces

Leave a Reply