mistux Site Admin
Joined: 25 Jun 2004 Posts: 1042 Location: South Bend, Indiana USA
|
Posted: Wed Mar 07, 2012 3:01 pm Post subject: Zip one or more files in Windows 7 |
|
|
Here is the inspiration for the following code:
http://forums.techarena.in/windows-server-help/997520.htm
Code: | '**This VB Script zips one or more files to a specified file
'**Works with Windows 7
'**Michael Mitchell
'**3-6-2012
'============================================================
Const FOF_CREATEPROGRESSDLG = &H0&
'**Path to where you want the zip file to go and the name of the .zip file
Const MyZip = "C:\temp\mynewzip.zip"
'**One or more files you want to zip up
'**Note that at the end you have to un-comment the additional files
Const File1 = "C:\temp\install2.log"
'Const File2 = "C:\..\File2.txt"
msgbox "About to crate ZIP file named: " & MyZip
'**before start : delete existing archive if it exists
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(MyZip) Then fs.DeleteFile(MyZip)
'-------------- 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
'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.
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
|
|
|