Script To Zip File

0
12

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.

  1. Download and install 7zip.
  2. Create script.
  3. Execute script.
  4. 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\”.

Note! If your data format is dd/mm/yyyy change tokens to 1,2,3.

Execute script.

At this point just run as administrator and the file will be archived.

Adding Time.

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

Also READ:

SHARE
Previous articleScript to delete files older than 7 days
Next articleWindows 7 / 10 – How to Delete Windows Update Files
mm
Hi, I’m Ravinder Jaiswal. I’m the person behind this website. Thank you for visiting the website! My website is all about Microsoft, VMware, Red Hat, Citrix and other technologies. I have 14+ years of IT experience. I am AZURE, MCSA 2016, MCSE 2016, RHCSA 7, RHCE 7, VCP-DCV 6.5, VCP-DTM 6.1, ITILv3 certified.