Automatically zip folders in separate archive files 7zip bat script batch processing separate folders to archive in separate zip files or 7zip files.

How to use

7zip auto archive batch script

Zip folders in separate archive files with 7-Zip and a batch file script in Windows 11.

This productivity life hack will speed-up your work when you need to archive multiple folders.

The batch script is with the extension .bat and it will do what you tell it to.

The Bat script

File name, you can name it anything .cmd I named it _7zipall.cmd

Batch file contents, file should have the .cmd extension to be executable.

Whole _7zipall.cmd script

You try this at your own risk, no liability accepted, no support.

for /d %%n in (*) do "%ProgramFiles%\7-Zip\7z.exe" a "%%n.zip" "%%n\"

The auto zip script explained:

For each sub directory loop

for /d %%n in (*)

goes thru each sub directory where the script is saved in / executed from. Then puts the directory details in a variable / parameter to be used in the next step.

Do run CLI program

do "%ProgramFiles%\7-Zip\7z.exe"

Run the 7z 7zip executable in the script – you should check your path, you might have to change this for it to work if you installed 7zip in a different directory.

7zip archive directory

 a "%%n.zip" "%%n\"

When calling the 7zip executable you pass the parameters telling it to add to archive (a) in a zip file named same as the sub directory and add the current folder in the loop to the zip file. We use the %%n variable that was set earlier.

Once 7zip finishes the current zip file, the script continues to the next directory and all steps are repeated until the last sub directory is zipped.

This script does not delete the original folders used for the zip files, you can delete those if you want. This is for safety, best to to a compare at the end if at least the count of folders and zip files is equal.

The script shown in the video will look at all directories in the current folder (where the script is saved) and
For each sub-directory it will create a new .zip file and add all the files contained in that directory to it’s zip file, then it moves to the next folder until it goes thru all the folders it found.

Archive files from sub-folders without creating the sub directory in the archive file

Updated 2024-03-18: Added the option to add to archive without creating a folder inside the zip archive file but it will create any sub folders if there are any under that.

Like this it skips the parent folder creation and puts the folder contents directly into the zip file.

If you have multiple directories and you want to archive all sub-folders into separate archive files but without creating a directory in the archive file, you can modify the path of the files to be archived.

Normally I would say better have the folder so that if you extract it, it goes back to the original folder structure. This is how I use it but you can change the script as below.

If this is what you want “.\%%n\*” instead of “%%n\” like below complete script:

for /d %%n in (*) do "%ProgramFiles%\7-Zip\7z.exe" a "%%n.zip" ".\%%n\*"



Comments

4 responses to “Auto zip folders in separate archive files 7zip bat script”

  1. Anton Kirilov Avatar
    Anton Kirilov

    Hello!
    When I use the script to zip multiple folders, I get multiple zip files as expected. In each zip file there is not only the contents of the source folder, but the source folder itself with its contents. Is it possible to change the script so that there is no folder inside the zip file, only the files contained in that folder?

    1. Normally I would say better have the folder so that if you extract it, it goes back to the original folder structure. This is how I use it but you can
      If you have multiple directories and you want to archive all sub-folders into separate archive files but without creating a directory in the archive file, you can modify the path of the files to be archived.

      If this is what you want “.\%%n\*” instead of “%%n\” like below complete script:

      I updated the post to include this but you should be sure this is what you really want.

  2. Matt Kersee Avatar
    Matt Kersee

    How about extracting file using WinRAR or 7-Zip command line method?

    1. Matt Kersee Avatar
      Matt Kersee

      The same method, if have a multiple archive, extract to separate folder either 7z, rar, zip, etc., thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.