Auto-Compressing Files with a Scheduled Batch Using 7-Zip

In our plant, we have a system where all kind of in-line actions are logged into trace files. But the trace files are overwritten when the file size reaches to the limits. These trace logs enables us to see the events of last 3 days. As you may guess, there are times when we need to analyze data from past, data older than 3 days. So I decided to set an automated mechanism that will compress and save the folder where all trace logs are stored and it will repeat this task every 3 days.

Here, I will try to explain how to automatically and periodically compress files in a specified folder using 7-Zip software which is a very popular and a free tool. I will try to define the procedure in steps and be as clear as possible:

Step 1

Download 7-Zip standalone console version from the 7-Zip web site download page:

https://www.7-zip.org/download.html

Note: The downloaded file will be a compressed file with 7z extension. To be able to extract the file, you will have to install 7-Zip software if you don’t have a tool which is compatible with 7z format.

Step 2

Extract and copy the file content to a path of your choice. e.g., C:\7zip or C:\Program Files\7zip. You don’t have to copy everything. You just need “7za.exe”, “7za.dll” and “7zxa.dll”. The 3 files in the root directory are for 32 bit (x86) version. If you want to use x64 version you need to use 3 files with the same name in folder named x64.

Note: If you have copied the files and everything is OK up to this point, you must be able to run “7za” command in windows command prompt when in the 7-Zip directory (The directory where you have copied the extracted files to).

Step 3

Create an empty txt file and rename it with extension .bat. I choose to use auto7zip.bat. This new file will be our batch command file. And copy the script below to your batch command file and modify it as you need.

Script Template:

[7-Zip Folder]\7za a -tzip "[Output Folder][Compressed File Name].zip" "[Folder to be Compressed]" -mx5

[7-Zip Folder] is the path to “7za.exe”, [Folder to be Compressed] is the path to the folder which you want to compress and save, [Output Folder] is the folder path where you want your compressed files to be stored in, [Compressed File Name] is the output file name which will be compressed with .zip extension. I choose to set the [Compressed File Name] with the current time stamp values which makes analyzing log history easier.

Sample Script:

C:\7ZipRoot\7za a -tzip "C:\MyArchives\MyZipFile.zip" "C:\LogsToBeArchived" -mx5

Sample Script with timestamp naming:

C:\7ZipRoot\7za a -tzip "C:\MyArchives\%DATE:~12,2%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.zip" "C:\LogsToBeArchived " -mx5

Note: When you are done with the batch file, you can double click on it and run the script to see if it works. If everything is fine, we can proceed with the next step.

Step 4

Open Windows task scheduler and create a new task. Name your task in “General” tab. Go to “Triggers” tab and create a new trigger which will recur every 3 days. Go to “Actions” tab and create a new action which will run the script in the batch file you have just created.

That’s it. Your files in the specified folder will be compressed and saved periodically.

Add a Comment

Your email address will not be published. Required fields are marked *