Wednesday, February 14, 2007

Zip Files with VBScript and WinZip

A couple of months ago I posted an entry about zipping and unzipping files with 7zip and VBScript. The script works well and I use it quite frequently and I definitely recommend it over WinZip if you have a choice.

I ran into a situation for a client where they needed a scripting solution but didn't want to use open source software because of a lack of support options and reluctance to move to an open source software model. Well in this situation I had to make due with what I had and I whipped up a script to zip files using WinZip. Sorry no UnZip yet, if someone really needs it feel free to send me an email or leave a comment.

This particular function relies on WZZIP.exe which is the command line equivalent of WinZip's gui interface. There are lots of command line switches you could add in if you desired other behavior. For example you can select to delete the files after they have been successfully compressed or select different levels of compression if you desired speed over minimum file size.

Anyway here is the function to zip files using WinZip instead of 7Zip.

Function Zip(sFile,sArchiveName)
  'This function executes the command line
  'version of WinZip and reports whether
  'the archive exists after WinZip exits.
  'If it exists then it returns true. If
  'not it returns an error message.

  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com


  Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
  Set oShell = WScript.CreateObject("Wscript.Shell")

  '--------Find Working Directory--------
  aScriptFilename = Split(Wscript.ScriptFullName, "\")
  sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
  sWorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")
  '--------------------------------------

  '-------Ensure we can find WZZIP.exe------
  If oFSO.FileExists(sWorkingDirectory & " " & "WZZIP.EXE") Then
    sWinZipLocation = ""
  ElseIf oFSO.FileExists("C:\program files\WinZip\WZZIP.EXE") Then
    sWinZipLocation = "C:\program files\WinZip\"
  Else
    Zip = "Error: Couldn't find WZZIP.EXE"
    Exit Function
  End If
  '--------------------------------------

  oShell.Run """" & sWinZipLocation & "wzzip.exe"" -ex -r -p -whs -ybc """ & _
  sArchiveName & """ """ & sFile & """", 0, True 

  If oFSO.FileExists(sArchiveName) Then
    Zip = 1
  Else
    Zip = "Error: Archive Creation Failed."
  End If
End Function
Send this to:                          

Comments

reena said...

Could you send me the code for unzip

Thanks

4/9/2007 11:48:44 PM

Nate+Rice said...

What code are you referring to? All of the code is above.

4/10/2007 10:54:53 PM

sandeep said...

Hey check out the following code if have to hard code the location of the file to be compressed.
Set oShell = WScript.CreateObject("Wscript.Shell")
sWinZipLocation = "C:\Program Files\WinZip"
oShell.Run """" & sWinZipLocation & "\WINZIP32.EXE"" -ex -r -p -whs -ybc """ & "C:\Program Files\Texas Instruments Inc\PurePath Studio\ComponentCache\Loudness_TI_v2" & """ """ & "\IResourceManager.dll" & """", 0, True
Zip = 0

This gives an error message "WinZip parameter validation error". Please suggest.

7/27/2007 6:45:58 AM

Nathan+Rice said...

here ya go:

Set oShell = WScript.CreateObject("Wscript.Shell")
sWinZipLocation = "C:\Program Files\WinZip"
oShell.Run """" & sWinZipLocation & "\WINZIP32.EXE"" -ex -r -p -whs -ybc ""C:\Program Files\Texas Instruments Inc\PurePath Studio\ComponentCache\Loudness_TI_v2\IResourceManager.dll""", 0, True
Zip = 0


Watch for unintentional line wrap here.

7/27/2007 9:45:50 AM

Tommy said...

Actually, I could use the unzip :)

2/11/2008 5:12:42 PM

Conan said...

Great code.

I'm a VBscript beginner and the oShell.Run command was all i needed.

YESSS

3/5/2008 7:58:19 AM

seb said...

Where about's do i enter the file location in this script for the file i want to zip, sorry novice at scripts

4/14/2008 1:34:53 AM

said...

Hello Nathan, I''m not a vbscript programmer and I need to use vbscript at work to make a script which automatically looks for files modified in the last 7 days, in a specific folder and all its subfolders, and then makes a zip saving all the paths, and moves the zip to a specific folder. Then we already have an application which moves the zip to another server, and we need another script in the other server, which unzips the file into the right paths... The purpose is to automate the copy of files from the production server to the DRP server (Disaster Recovery Plan)... The servers have Windows 2000 installed, and I should use WinZip. I think I could use part of your script, but I should add a lot of code for looking into all the folders for the modified files... Do you have anything like that (to use as an example) in any script? Thanks very much! Daniela.

7/16/2008 7:37:38 AM

said...

sorry about my other comment on the vbscript with 7zip..i just notice that u have implemented it in winzip too...
anyway, can i ask for codes in vbscript for unzipping file?im still new in vbscript so i need some help...plsss....

7/28/2008 3:53:03 AM

said...

Hi There,I have used your zipping function using WinZip with success.Could you provide the code for unzip as well.Im stuck in my file transfer task,as Im not sure on the function.Thanks

7/30/2008 7:25:56 AM

said...

Hi,
is it possible to add a line of code that will encrypt the data into this script?
Really struggling to do it myself

thanks

8/5/2008 8:29:20 AM

said...

Hi.

Is it possible to zip all files within a folder by there modified date.

For example.

FileName Mod date
1.log 03/09/2008
2.log 03/09/2008
3.log 03/09/2008
4.log 04/09/2008
5.log 04/09/2008

Would create

2008-09-03.zip - 3 files
2008-09-04.zip - 2 files

Thanks

9/4/2008 3:38:41 AM

said...

I have a slightly different issue. I get files ftp''d to me that were compressed - filename test.txt.Z - and on the PC I need to unzip them. I really need to control via script due to file system & naming activities I also do at the same time. With WinZip 10 and command line add-in I am out of luck - no support for unix files. However, if I double-click the archive file, WinZip launches, I select button, click button again (selecting location of where extracted file should go) - and the file is extracted and is perfectly useable. How can I ''automate'' so that I do not have to click the buttons? Newbie - but willing to try. And I must stick with WinZip - not allowed to use free/shareware in my environment. Thanks in advance!

10/10/2008 8:13:02 AM

said...

Is the principle the same with wzunzip.exe??

1/13/2010 6:33:45 AM

said...

Hello,
Thanks for this great example. I would like to find a method to add files to a zip archive based on time instead of day.

5/5/2010 9:09:54 AM

said...

Thanks, just what I was looking for

9/7/2010 6:44:44 AM

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above