Solved

vbscript: How can you create a zipped folder?

Posted on 2004-09-28
11
2,014 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

777 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