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

 

2 thoughts on “Zip Files with VBScript and WinZip”

  1. I need an unzip self-extract program that will run from VBScript. Also I would like to wait until it is complete, then I can display a message box to the user to the let them know it is done. I create a silent winzip with the following command from DOS:

    WZIPSE32 “ISS.zip” -silent -d “C:\ISS\”

    How can I run this with VBScript? Thanx in advance.

    Jerry

    Reply
    • Hi Jerry, This isn’t really a great forum for such a general question. I suggest you hire a programmer if that’s what you require.

      Reply

Leave a Comment