Solved

vbscript: How can you create a zipped folder?

Posted on 2004-09-28
11
2,000 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:danwinson
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:danwinson
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now