How to compress directories in ZIP archives using 7zip and CMD

Views: 11.927
Reading Time: 3 minutes

Hello people,
Good evening.

In this quick post I will demonstrate how to compress directories and sub directories by creating 1 zip file with all content or 1 ZIP file per directory using the 7-Zip, the best file compactor, in my opinion, and the DOS prompt (CMD).

I use this little script a lot in my daily life, mainly to store the scripts and logs of the requests and projects I attend. This week I had to reuse these scripts when I generated large CSV files (over 2 GB per file) with lots of information extracted from the database using the stored procedure. stpExporta_Query_Txt, available in post SQL Server - How to export database data to text file (CLR, OLE, BCP).

To make these files available to the requester, I wanted to compress them into 1 ZIP file for each file generated, but they were 65 CSV files, which would take me a little time.

The solution to this problem was to resort to one of the scripts below, which I prepared for 3 types of needs. In these examples below, I will use the following structure:

How to compress cmd batch directory files

Create 1 ZIP File for Each Subdirectory

In this first example, I will demonstrate how to read all directories in the current folder and create 1 zip file for each directory in the current folder. The sub directories will be part of the created Zip file.

Source code:

Processing:
How to compress 2 batch cmd directory files

Final result:
How to Compress Batch Directory Files cmd Result_2

Create 1 ZIP File with Subdirectory Contents

In this second example, I will demonstrate how to read all directories in the current folder and create 1 zip file with this content. The sub directories will be part of the created ZIP file.
Source code:

Processing:
How to compress 4 batch cmd directory files

Final result:
How to Compress Batch Directory Files cmd Result_3

Create 1 ZIP File for Each Root File

In this third and last example, I will demonstrate how to read the files that are in the current folder and create 1 zip file for file. Directories and sub directories are not considered, only files that are in the root.

Source code:

Processing:
How to compress 3 batch cmd directory files

Final result:
How to compress batch directories cmd result2 files

And that's it folks!
Hope this post is useful for you too!

Hug!

how to compress batch batch files via command line command command line compress files directory folders shell cmd prompt

how to compress batch batch files via command line command command line compress files directory folders shell cmd prompt