Results 1 to 11 of 11

Thread: How to create ZIP Files from Command-Line via VBScript?

  1. #1
    FB Guest

    How to create ZIP Files from Command-Line via VBScript?

    Ho can i create Zip Files via command-line?
    Can be done via WMI?

    I don´t wann use WinZIP COmmand Line Add-On, because it is not freeware

    Can i use VBScript to do that? Other "Lnaguage" maybe?

  2. #2
    Wayne Tilton Guest

    Re: How to create ZIP Files from Command-Line via VBScript?

    How about a free command line ZIP utility instead of WinZIP?

  3. #3
    LJB Guest
    Since Windows already knows how to deal with zip files to some extent you
    might want to try the following. No external programs are needed. Believe it
    or not the wscript.sleep at the end is important to make this work
    correctly.

    Const FOF_CREATEPROGRESSDLG = &H0&

    Const MyZip = "C:\..\MyZipFile.zip"

    Const File1 = "C:\..\File1.txt"
    Const File2 = "C:\..\File2.txt"

    Const MyDest = "C:\scratch"

    '-------------- create empty zip file ---------

    'Create the basis of a zip file.
    CreateObject("Scripting.FileSystemObject") _
    .CreateTextFile(MyZip, True) _
    .Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)


    '-------------- zip ---------------------------

    'get ready to add files to zip
    With CreateObject("Shell.Application")

    'add files
    .NameSpace(MyZip).CopyHere File1, FOF_CREATEPROGRESSDLG

    .NameSpace(MyZip).CopyHere File2

    End With
    wScript.Sleep 1000

    Here is some info you may find useful too

  4. #4
    FB Guest

    Re: How to create ZIP Files from Command-Line via VBScript?

    Tanks, i´ll try it
    In this forum i found another Tip, in creating a Blank ZIP file with "PK"
    bytes and 17 bytes with CHR(0)

    I´ll try to see

    ============================================
    Set Ag=Wscript.Arguments
    username = CreateObject("Wscript.Shell").Environment("Process")("username")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts =
    fso.OpenTextFile(FilePath&"_forms\workforus\_uploads\"&"form_data.zip", 2,
    vbtrue)
    BlankZip = "PK" & Chr(5) & Chr(6)
    For x = 0 to 17
    BlankZip = BlankZip & Chr(0)
    Next
    ts.Write BlankZip
    set objFolder = nothing
    set objShell = nothing
    Set fso = nothing
    Set ts = nothing

    Set objShell = CreateObject("Shell.Application")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set
    DestFldr=objShell.NameSpace(FilePath&"_forms\workforus\_uploads\"&"form_data.zip")
    Set SrcFldr=objShell.NameSpace(FilePath&"_forms\workforus\_csv")
    DestFldr.CopyHere (FilePath&"_forms\workforus\_csv")
    ============================================

  5. #5
    FB Guest
    It works great for small files, for large files i´ve added a Wscript.Echo
    (Modal Button, with OK) and ionly works if i click the OK button after the
    Zip proccess ( i can see a dialog box with a gauge indicating that the zipins
    in on the way

    Maybe it be necessary to use a higher number in the sleep directive

    What´s the purpose of the Const MyDest = "C:\scratch"???
    In any circunstances this line is used for something?

    Const MyDest = "C:\scratch" was something I used and forgot to remove before
    sending the example.

  6. #6
    Join Date
    May 2009
    Posts
    1

    Re: How to create ZIP Files from Command-Line via VBScript?

    Hi, Your script really helped me... But how can i use wild cards like i want to add C:\*.id file to the created zip file how wud i do that n this script

  7. #7
    Join Date
    Jun 2009
    Posts
    2

    Re: How to create ZIP Files from Command-Line via VBScript?

    (duplicated post)
    Last edited by dunwich; 07-06-2009 at 04:31 AM.

  8. #8
    Join Date
    Jun 2009
    Posts
    2

    Re: How to create ZIP Files from Command-Line via VBScript?

    Hi

    Thank you for the code sample, it's quite useful.
    Let me complete it a bit.
    The way it is coded, the compression will run in background. If there is a lot of data to archive, the script will end while the compression is on-going, and the end of the script will destroy the compression object and cancel the compression.

    Here is a bit of code to wait until the compression is finished.

    ' Add this in the begining:
    ' before start : delete existing archive if it exists
    Dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.FileExists(MyZip) Then fs.DeleteFile(MyZip)

    ' add this after the compression code from LJB


    'the CreateTextFile will create the file with 22 bytes inside
    ' the NameSpace(MyZip).CopyHere will tell windows to insert compressed files in it. For that, windows will replace the file by some temp file, and the file will reappear only when it's finished.
    ' so the compression is finisshed when I can see the file, with a size > 22 bytes.
    ' code :

    Dim fZipIsFinished
    fZipIsFinished=0
    While fZipIsFinished=0
    If fs.FileExists(MyZip) Then
    Dim h
    Set h=fs.getFile(MyZip)
    If h.size=22 Then
    fZipIsFinished=0
    ' File created, compression did not start yet
    Else
    fZipIsFinished=1
    'File created, compression finished
    End If
    Else
    'File removed for compression, not yet recreated. compression is in progress
    fZipIsFinished=0
    End If
    wScript.Sleep 1000
    Wend

    msgbox "Done"

    'Limitation : if the compression is canceled (on the progress window),
    'the script will not know it and wait forever. hence my remark below.

    ' end code

    that's it. Hope it helps.

    I wish I know how to remove the progress window, and make it ignore the read errors, because I want to use it to backup big file-systems, as a subtitute for tar.
    But anyway for me, it does not quite reach my needs, because I need to produce multivolume, and in STORE compression mode, and with full windows attribute. Perl, cygwin etc are no good because they do not store all windows attributes.

    > How about a free command line ZIP utility instead of WinZIP?

    Ye's, there are the command line version of 7z, and zip.exe from Info-Zip. Both are free and great, but they do not do multivolume.
    winzip does it but is not free. I have an old license of pkzip, but for Windows 3.1. Can you imagine ? It stores the files with names in 8.3 !

    So I am still seacrhing...
    Suggestions are welcome.

  9. #9
    Mark D. MacLachlan Guest

    Re: How to create ZIP Files from Command-Line via VBScript?

    Have a look near the end of this thread:
    http://www.tek-tips.com/viewthread.cfm?qid=1231429. Sample code is
    given that should help you.

    Hope that helps,

    Mark D. MacLachlan

  10. #10
    Join Date
    Apr 2011
    Posts
    1

    Re: How to create ZIP Files from Command-Line via VBScript?

    This thread has been helpful and I'd like to add the way to wait for the compression to end.

    This seems to work:

    Set file = winShell.NameSpace(zipFilePath)
    file.CopyHere(pathToFile)

    Do Until file.Items.Count >= 1
    Wscript.Sleep(200)
    Loop

    In this example I'm adding only one file so the check is >= 1

  11. #11
    Join Date
    Dec 2011
    Posts
    1

    Re: How to create ZIP Files from Command-Line via VBScript?

    Here is my solution for determining when the file is done zipping. It seems to work so far...

    Code:
    'Get the current size of the zip file.
    Set zipFile = fileSystemObject.GetFile(zipFilePath)
    zipFileSize = zipFile.Size
                        
    'Add the file to the zip archive.
    CreateObject("Shell.Application").NameSpace(zipFilePath) _
        .MoveHere file.Path, FOF_CREATEPROGRESSDLG 
                            
    zipFinished = False
                           
    'Wait for the file to finish zipping before we continue.    
    Do Until zipFinished
        If fileSystemObject.FileExists(zipFilePath) Then
            If  zipFile.Size > zipFileSize Then zipFinished = True
        End If
    Loop

Similar Threads

  1. How to create a .ISO File and burn a CD, via command-line
    By Dubeyji in forum Windows Software
    Replies: 6
    Last Post: 15-09-2010, 06:00 AM
  2. Create torrent-file via command line
    By GurdeepS in forum Technology & Internet
    Replies: 8
    Last Post: 23-06-2010, 06:52 AM
  3. Winrar: Create SFX via Command Line
    By magicT in forum Windows Software
    Replies: 3
    Last Post: 03-07-2009, 01:26 PM
  4. Create shortcuts in the command line
    By DarenHawk in forum Software Development
    Replies: 4
    Last Post: 27-04-2009, 11:11 PM
  5. How to create a user webmin via command line in Linux
    By Elsie in forum Networking & Security
    Replies: 3
    Last Post: 25-04-2009, 06:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,007,543.75611 seconds with 17 queries