Link to home
Start Free TrialLog in
Avatar of danwinson
danwinson

asked on

vbscript: How can you create a zipped folder?

I need to compress event log files using a script, but don't want to use NTFS compression (CIM_DataFile .Compress method).

How can I script the use of the built in 'compress folder' (zipfldr.dll) feature of Windows Server 2003?
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

As far as I am aware, you cannot call functions from .DLLs within a script, you have to use a compiled VB application.

The following is a link to a class that can be used in a VB6 application:
    http://www.mvps.org/emorcillo/vb6/shell/xpzip.zip

Otherwise, you should download a command-line archiver, such as 7-Zip, and use that.  You can get 7-Zip from:
    http://www.7-zip.org/

HTH
Sorry, missed a bit:

>> Otherwise, you should download a command-line archiver, such as 7-Zip, and use that.

Otherwise, you should download a command-line archiver, such as 7-Zip, and use a Shell object to run that.
Avatar of yuvaprakash
yuvaprakash

there are lot of ipp & unzip wares available for free download...DOS shell executables

U can also use PKzip and PkUnzip..u can also find some example codings for that..
One of the nice tool i used in my previous applications..try n tell me

cheers
Avatar of danwinson

ASKER

While the comments received were to an extent helpful, I do not have the option of installing third party software so they don't fix my problem (or answer the question). Thanks everyone for your help though. Can I please have the points refunded.
From http://www.mvps.org/emorcillo/vb6/shell/xpzip.shtml:

    "There's no API in Windows to work with .zip files but the compressed folder file extension can be used from VB to
    compress and decompress files in .zip files."

This suggests that you may be able to simply make a folder (with the FSO) called something.zip and it will make the zip file.

To test this, try:
   
     Dim oFSO, oFile, oFolder
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFolder = oFSO.CreateFolder("x:\mydir\myzipfile.zip")
     
     Set oFile = oFSO.GetFile("x:\mydir\myfiletoadd")
     oFile.Copy oFolder.Path

HTH

J.
I tried your code but it seems to just create a folder called myzipfile.zip, it doesn't create a zip file. Please let me know if I might be doing something wrong.

I checked out the link to mvps, it does provide info on how to do this using a VB6 class, but I don't know if this code can be scripted, I can't load any new .exe files onto the server in question, only vbs files. What sort of portability is there between VB6 code and vbscript?
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the above answer didn't exactly answer my question, but the effort put in by jimbobmcgee, the clarity of the response and the fact that it effectively rules out any other correct answer to my response deserves the points.
There is a solution using VBScript only:

Const FOF_CREATEPROGRESSDLG = &H0&

With CreateObject("Shell.Application")
    For each Fldr in SrcFldrs

          'Copy the files to the compressed folder
          .NameSpace(ZipFile).CopyHere .NameSpace(Fldr).Items, FOF_CREATEPROGRESSDLG

          iFiles = iFiles + .NameSpace(Fldr).Items.Count

          'Keep script waiting until Compressing is done
          On Error Resume Next
          Do Until .NameSpace(ZipFile).Items.Count = iFiles
              wScript.Sleep 1000
          Loop
            On Error GoTo 0

      Next
End With
I can confirm that the above approach works, with one caveat; you must create the zip file first.  As such, the following slight alteration would apply.

Const FOF_CREATEPROGRESSDLG = &H0&

Dim ZipFile : ZipFile = "C:\vbscripted.zip"
Dim SrcFldrs : SrcFldrs = Array("D:\Documents and Settings\jimbobmcgee\My Documents")

With CreateObject("Scripting.FileSystemObject")
      .OpenTextFile ZipFile, 2, true
End With

With CreateObject("Shell.Application")
    For each Fldr in SrcFldrs

          'Copy the files to the compressed folder
          .NameSpace(ZipFile).CopyHere .NameSpace(Fldr).Items, FOF_CREATEPROGRESSDLG

          iFiles = iFiles + .NameSpace(Fldr).Items.Count

          'Keep script waiting until Compressing is done
          On Error Resume Next
          Do Until .NameSpace(ZipFile).Items.Count = iFiles
              wScript.Sleep 1000
          Loop
            On Error GoTo 0

      Next
End With

Shame its 5.5 years after the OP, though...