Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip a folder up

Tags:

zip

vbscript

I am trying to ZIP up a folder in VBScript and it doesn't seem to work. I'm certain I am creating the header file correctly.

It creates the actual file correctly, just doesn't zip the folder.

Anyone got any ideas:

Sub ArchiveFolder (folder)

    Dim fso, wShell, sApp, zipFile

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wShell = CreateObject("WScript.Shell")  
    Set sApp = CreateObject("Shell.Application")
    Set zipFile = fso.CreateTextFile(folder & ".zip")

    ' Write zip file header.
    zipFile.Write "PK" & Chr(5) & Chr(6) & String(18, 0)
    zipFile.Close

    sApp.NameSpace(folder & ".zip").CopyHere folder

End Sub
like image 991
Cheetah Avatar asked Feb 28 '13 15:02

Cheetah


People also ask

Can I zip a folder with folders in it?

You could do the following: Open a folder window and right click on the level2 folder. From the WinZip submenu choose Add to Zip file. Click Add.

What does it mean to zip a folder?

ZIP files work in much the same way as a standard folder on your computer. They contain data and files together in one place. But with zipped files, the contents are compressed, which reduces the amount of data used by your computer. Another way to describe ZIP files is as an archive.

What does it mean to zip up a file?

A zip file is a way of grouping, or archiving, multiple files so they act like one file. For example, let's say you want to email a folder of Word documents to someone. You could attach each file individually, but it would take a long time—especially if there are a lot of documents.


1 Answers

The answer I found here. The magic is in the last Do..Loop where the script wait the Shell to do it job.

ArchiveFolder "sub\foo.zip", "..\baz"

Sub ArchiveFolder (zipFile, sFolder)

    With CreateObject("Scripting.FileSystemObject")
        zipFile = .GetAbsolutePathName(zipFile)
        sFolder = .GetAbsolutePathName(sFolder)

        With .CreateTextFile(zipFile, True)
            .Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))
        End With
    End With

    With CreateObject("Shell.Application")
        .NameSpace(zipFile).CopyHere .NameSpace(sFolder).Items

        Do Until .NameSpace(zipFile).Items.Count = _
                 .NameSpace(sFolder).Items.Count
            WScript.Sleep 1000 
        Loop
    End With

End Sub
like image 53
Panayot Karabakalov Avatar answered Sep 25 '22 00:09

Panayot Karabakalov