Zip it Good: A Fun and Easy Batch Script for Compressing Your Files
Efficiently Compress Your Files with 7-Zip: A Step-by-Step Batch Script Tutorial
Have you ever had a ton of files to send to someone, but didn't want to overwhelm them with a massive email attachment? Or maybe you just need to clean up your own computer and want to condense a bunch of files into smaller packages. Well, fear not my friend, because this batch script using 7-Zip is here to help!
Picture this: you have a folder full of files that you want to send to someone or organize for yourself. You open up the folder and start selecting files to add to a zip archive. But wait, there's too many files! You don't want to create a zip archive that's too big, or your recipient might never receive it. That's where this batch script comes in.
With just a few lines of code, this script takes care of everything for you. It starts by setting a limit on the number of files that should be included in each zip archive. This way, you won't have to worry about creating a zip archive that's too big or too small. It then goes through each file in the input folder, adding it to a zip archive until the limit is reached. Once the limit is reached, it creates a new zip archive and continues adding files to that one.
@echo off
setlocal EnableDelayedExpansion
set folder="C:\path\to\input\folder"
set count=0
set file_count=0
set max_files_per_zip=20
set output_folder="C:\path\to\output\folder"
for %%f in (%folder%\*) do (
set /a file_count+=1
if !file_count! gtr !max_files_per_zip! (
set /a count+=1
set file_count=1
)
set zip_file=!output_folder!\archive_!count!.zip
if not exist "!zip_file!" (
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx=9 "!zip_file!" "%%f"
) else (
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx=9 "!zip_file!" "%%f" -aoa
)
)
echo %count% zip files created.
And the best part? You don't have to do anything but run the script! It's like having your own personal zip archiving assistant. Plus, it uses 7-Zip, which is a powerful and efficient compression tool that ensures your files will be compressed as much as possible.
So next time you need to send a bunch of files, or just want to clean up your own computer, give this batch script a try. It's easy, efficient, and might just save you a ton of time and effort. Who knows, you might even have fun zipping things up!
The technical bit
First, the script sets a limit on the number of files that should be included in each zip archive. This limit is stored in the variable "max_files_per_zip". The default value is 20, but you can change it to suit your needs.
Next, the script goes through each file in the input folder and adds it to a zip archive. It keeps track of the number of files that have been added so far using the "file_count" variable. Once the limit is reached, it creates a new zip archive and continues adding files to that one.
The script also keeps track of the number of zip archives that have been created using the "count" variable. Each new zip archive is named "archive_!count!.zip", where !count! is the current value of the "count" variable. This ensures that each zip archive has a unique name.
The script uses 7-Zip to create the zip archives. If a zip archive with the current name already exists in the output folder, the "-aoa" flag is used to overwrite it without prompting the user. Otherwise, a new zip archive is created using the "-mx=9" flag to maximize compression.
Once the script has finished running, it prints the number of zip archives that have been created to the console using the "echo" command.
Overall, this batch script provides a simple and efficient way to create zip archives for large sets of files. By automating the process, it can save you time and effort while ensuring that your files are compressed as much as possible.