Solved

vbscript: How can you create a zipped folder?

Posted on 2004-09-28
11
2,017 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:danwinson
11 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12178063
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
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12178070
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.
0
 

Expert Comment

by:yuvaprakash
ID: 12178564
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:danwinson
ID: 12560097
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.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12697733
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.
0
 

Author Comment

by:danwinson
ID: 12700616
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?
0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
ID: 12704657
>> I tried your code but it seems to just create a folder called myzipfile.zip

I figured as much; I didn't hold out much hope for that...

VB6 to VBS is a nightmare; it's like someone took VB6 and said, "This is nice, let's absolutely cripple it and give it to our adoring public -- they'll buy anything!!" ;-)

Seriously, though, you can't use class modules in a VBS, neither can you use API calls.

I don't think the zipfldr.dll library exposes any ActiveX objects -- I couldn't add a reference to it in VB6 and that's usually a bad sign.

So I went on a hunt for a .DLL that would and came up with an ASP-based one at http://www.chestysoft.com.  I registered that .DLL with regsvr32.exe and after a little play with that, I had something that would create a simple zip file for me:

      Dim myZip
      Set myZip = CreateObject("csASPZipFileTrial.MakeZip")  'CHANGE THIS TO "csASPZipFile.MakeZip"
                                                                                        'IN FULL VERSION
      myZip.ZipAdd "x:\mypath\myfile.txt"
      myZip.SaveZip "x:\mypath\myzip.zip"

      Set myZip = Nothing

So, I don't think you can use zipfldr.dll, as it contains no ActiveX object and .VBS files cannot declare API calls, but you can use this .DLL (as long as you can register it -- permissions, etc).

HTH

J.
0
 

Author Comment

by:danwinson
ID: 12713001
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.
0
 
LVL 7

Expert Comment

by:timker
ID: 27303327
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
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 27511232
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...
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question