Create individual Zip files with a command line

3 users found this article helpful

Individual Zip files in a folder of files

To have WinZip Command Line Support Add-On create individual Zip files of each file in a folder, open a Command Prompt window and change directories (CD) to the folder where the files to be zipped are located.

After changing directories, you can enter the following two lines in the Command Prompt window:

path=%path%;"c:\program files\winzip"
for %f in (*.txt) do wzzip %~nf.zip %f

Or enter the following two lines in a batch file:

path=%path%;"c:\program files\winzip"
for %%f in (*.txt) do wzzip %%~nf.zip %%f

If your filenames include spaces, you will need to enclose the 2nd, 3rd, and 4th variables in quotes. For the example using the Command Prompt window, it would now look like this:

path=%path%;"c:\program files\winzip"
for %f in ("*.txt") do wzzip "%~nf.zip" "%f"

Individual Zip files of folders

It is also possible to create a set of Zip files, where the contents of all the subfolders in a folder will be zipped into individual Zip files. This set of commands contains a few steps and will not work directly from a command prompt. They must be saved in and run from a batch file.


REM create list of directories
CD C:\[PATH1]
DIR /AD /B >dirlist.txt

REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
  CD "%%a"
  wzzip "C:\PATH2\%%a.zip"
  CD..
)

Note: The final line above the ending parenthesis is the CD command, followed immediately by two dots.

Running the commands above as a batch file, will create a text file with the name of each directory (folder) on separate lines. The second section reads the text file, changes directories to the one on the line it just read, and performs the wzzip command once for each of these lines in the text file. The [PATH1] item should be replaced with the actual path to the parent folder, which contains the subfolders you want to zip. The [PATH2] item should be replaced with the path to the target folder where you want to save your Zip files. Other switches can be added to the wzzip command if desired. For example, you can add "-r -p" just after wzzip if the folders have subfolders inside, and you want their contents added as well.

You may want to add a command at the end of your batch file to delete the dirlist.txt file. This is optional and would depend on your preferences.

Was this article helpful?

Tell us how we can improve it.