In this tutorial, we will explain how you can create a batch script to zip file using the cmd command. 7zip is a program that will use for zipping. We are using batch files because are easy to create, popular, and can operate on several versions of Windows and DOS.
How to create a batch script to zip file.
- Download and install 7zip.
- Create script.
- Execute script.
- Adding Time.
- Batch to delete file older than– Delete files older than 7 days using batch and script.
- Batch to delete file automatically – Delete the file using the command line.
- Batch to delete folder – Delete the folder using the command line.
Download and install zip file:
First, go to http://www.7-zip.org/download.html and download the program for your system operation.
Create a batch script to zip file.
At this point, we will create a batch file to compress files. Open the text file and copy the below command. Finally, save as zipping.cmd.
echo on for /f "tokens=3,2,4 delims=/- " %%x in ("%date%") do set d=%%y%%x%%z set data=%d% Echo zipping... "C:\Program Files\7-Zip\7z.exe" a -tzip "D:\dmp\Test_Zipping_%d%.zip" "D:\dmp\*.dmp" echo Done!
In the first lines, we are declaring “date” to create a zip name later. For example, zipping oracle database backup.
With command “D:\dmp\*.dmp” the script will archive all files (*) with an extension .dmp in the folder D:\dmp\. This can be changed with your file extension.
“D:\dmp\Test_Zipping_%d%.zip” – Test_Zipping the name that we want for the archive adding %d% the actual date.
- “C:\Program Files\7-Zip\7z.exe” – (required) starts the 7-Zip command-line executable.
- a– (required) command to add files to the archive.
- -tzip– (optional) switch to set the type of archive; in this case, it’s a zip file (optional unless using another compression format).
- “D:\dmp\ Test_Zipping _%d%zip” – The name and the path where the archive file will be saved.
- “D:\dmp\*.dmp” – The files that will be archived
Also, you can use “\*.*” instead of “\*.dmp” in batch file to compress all files with an extension in the folder “D:\dmp\”.
Note! If your data format is dd/mm/yyyy change tokens to 1,2,3.
At this point just run as administrator and the file will be archived.
However, if you want to add the time after the date in the name of the archive you can include the below lines at the start of the script.
for /f "tokens=1,2,3 delims=:. " %%x in ("%time%") do set t=%%x%%y%%z set time=%t%
Add the %t%:
“D:\dmp\Test_Zipping_%d%_%t%.zip” will be product Test_Zipping_06102016_090304.zip
Other useful batch tutorials:
If you have any questions feel free to ask in the comment section.
Please rate use if this article was helpful to you!