Solved

vbscript: How can you create a zipped folder?

Posted on 2004-09-28
11
2,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

751 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