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:
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:
1 2 | for /d %X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%X.zip" "%X\" pause |
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:
1 2 | for /d %X in (*) do "C:\Program Files\7-Zip\7z.exe" a "Arquivo.zip" "%X\" pause |
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:
1 2 | for %X in (*.txt) do "C:\Program Files\7-Zip\7z.exe" a "%X.zip" "%X" pause |
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
how to add a date and time in Create 1 ZIP file with the contents of the sub-directories
Great. During compression, can you rename compressed folders?
Congratulations, your codes have helped me a lot. It was pretty hard to make a Bat using 7Zip and you got it.
Perfect post… even more for using a free solution…
but because I didn't explain the code, I can't change the location of the ZIP archive creation.
Create the ZIP file in another directory.
Oops cool post, would you have any sql server backup script that generates the zip file? or in rar?
Thanks for the feedback, Jorge! Soon, I will post to compress files in SQL Server. Wait… lol