In this tutorial we will explain how you can create script to zip file using cmd command. 7zip is 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 script to zip file.
- Download and install 7zip.
- Create script.
- Execute script.
- Adding Time.
Download and install zip file:
First go to http://www.7-zip.org/download.html and download the program for your system operation.
Create script to zip file.
At this point we will create batch file to compress files. Open 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!
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\”.
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 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
- Batch to delete files older than 7 Days – Delete files older than 7 days using batch and script.