Automatically create Zip Files of all sub-directories from the command line using 7-Zip
This code shim explains how to create a batch script that will let you automatically create zip files of all directories inside a parent directory. I regularly use this script to compress log files that are stored on a monthly basis in a logging directory, with a single zip per month of logs.
This code shim depends upon you using: Windows (XP+), 7-Zip
Preparation
- Create a folder on your computer to place utilities into. For the purpose of this guide, I will use the path: “C:\_DevBin”
- Download 7-Zip from 7-zip.org and install it. Make a note of the folder path it installs to (by default: “C:\Program Files\7-Zip” or “C:\Program Files (x86)\7-Zip” depending on your system architecture and version downloaded).
ZipSubs code
@
echo offfor /f “usebackq tokens=*” %%a in (`dir /b/a:d`) do (
7z a -r %%~nxa.zip %%~nxa
)
Open a new notepad window, paste in the sample code and save the document as C:\_DevBin\ZipSubs.bat
Make the tool accessible from anywhere
Once you have a local folder with the ZipSubs file in, open your system’s Environment Variables dialog. This can vary from different versions of Windows, so you may need to check online for the exact method.
When you have the Environment Variables dialog open, edit the PATH System Environment Variable, and add to the start:
This will make all binaries and executables in the 7-Zip and _DevBin folders available from any command prompt.
Test out your utility!
Open a new command prompt window and navigate to a folder that contains sub-folders. Type “ZipSubs” (without quotations) and hit enter. You should see your folders then get compressed into zip files!
Download the Utility
Here’s one I made earlier – a download containing the code and a quick readme.