7-zip by default creates a single archive when multiple files or folders are selected to be compressed. It’s however possible to instruct the program to compress each file or folder to a separate archive instead.
This however has to be done over command line since the program has no option in its interface to specify this. This is unlike WinRAR which provides an option to put each file to a separate archive in its compression settings.
The 7-zip commands are however simple and can optionally be saved for later use in a batch file that requires no command line input. Let’s take a look how to use the commands.
Add 7-Zip to Environment Variables
For ease of use, you can add 7-zip as an environment variable so that you don’t have to specify the path to its executable in the command. Otherwise, you may get the error “7z is not recognized as an internal or external command, operable program or batch file” when you run the commands in this page.
To do that:
- In Windows 10/11, go to the search bar and enter environment variables then click on the option Edit the system environment variables. You can also open the System Properties window first by entering
sysdm.cpl
in the run window (Windows key +R) then go to the Advanced tab. This latter option will work on any version of Windows. - Click on the Environment Variables button.
- Select the Path option under the System variables section, then click the Edit button.
- Next, click on Browse and select the 7-zip installation folder containing the 7-zip.exe. The default folder is typically
C:\Program Files\7-Zip
however it could also be inC:\Program Files (x86)\7-Zip
if you installed the 32-bit version on a 64-bit Windows version. - Click OK to save the path and exit.
Commands to Compress Folders to Separate Archives
Open the command window in the folder containing the sub-folders you want to compress, then use the respective command below.
To compress all the sub-folders in the directory to separate ZIP archives, use:
for /D %d in (.) do 7z a -tzip "%d.zip" ".\%d*"
The above command will compress the folder to the root of the archive. If you want to include the folder in the archives, use the following command instead:
for /D %d in (.) do 7z a -tzip "%d.zip" "%d"
To compress using the 7z format instead of ZIP use:
for /D %d in (.) do 7z a "%d.7z" ".\%d*"
Or:
for /D %d in (.) do 7z a -t7z "%d.7z" ".\%d*"
Note: If you haven’t added 7zip to the path you may specify the executable manually within the command as follows:
for /D %d in (.) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%d.zip" ".\%d*"
You can use the above commands in a batch file as well, however they require a slight adjustment for them to work properly. To do that, open notepad and paste the command then replace each %
with %%
.
For example, to compress all the sub-folders in the directory to separate ZIP archives the command becomes:
for /D %%d in (.) do 7z a -tzip "%%d.zip" ".\%%d*"
Save the file as a .bat file then put it in the directory containing the folders to compress. You can then double-click or drag a sub-folder on to it to compress all the sub-folders inside that directory.
You can also put the bat file in the desktop or some other location for future use. That way, any time you need to compress the sub-folders in a given directory, you’ll only have to drag and drop one of the folders into the bat file for the command to run.
Commands to Compress Files to Separate Archives
To compress all the files in a directory to separate ZIP archives use:
for %i in (.) do 7z a -tzip "%i.zip" "%i"
To compress the files using the 7z format instead of ZIP use:
for %i in (.) do 7z a "%i.7z" "%i"
Or:
for %i in (.) do 7z a -t7z "%i.7z" "%i"
The above commands will compress ALL the files in the directory. To restrict the files to a specific file type, you can specify its extension in the command as follows:
for %i in (*.jpg) do 7z a -tzip "%i.zip" "%i
The above command will compress only JPG files. To compress a different format, replace *.jpg with its extension.
The above commands will also include the extension in the archive names. For instance, 1.jpg will be compressed to 1.jpg.zip. To exclude the extension from the archive’s file name use the following commands instead:
for %i in (.) do 7z a -tzip "%~ni.zip" "%i"
For 7z format:
for %i in (.) do 7z a "%~ni.7z" "%i"
And with an extension specified it will be:
for %i in (*.jpg) do 7z a -tzip "%~ni.zip" "%i"
You can also use a batch file to compress files as it was the case with the folders. You’ll however need to replace the single % with %% for the commands to work in the .bat file.
For example, to compress all the files in the directory to separate ZIP archives the command becomes:
for %%i in (.) do 7z a -tzip "%%i.zip" "%%i"
And the command that excludes the extension in the archive’s file name becomes:
for %%i in (.) do 7z a -tzip "%%~ni.zip" "%%i"
Note: When excluding the extension from the archives’ file names, make sure the directory does not contain different file types with similar names. This causes a conflict since both files will have the same archive name. For example, 1.txt and 1.jpg will both be compressed into 1.zip. To avoid this conflict, restrict the compression to specific extensions or move the conflicting files to another folder prior to using the commands.