Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compress to Zip 1

Status
Not open for further replies.

NetNodeMan

Technical User
Mar 30, 2005
65
IE
Hi folks,

Have had a search through the forum but the following is bugging me. I'm looking for some code to compress a certain folder to Zip and then email it. I have no problem sending the mail but the zipping of the files is proving difficult. Can anyone post a working program that zips the content of C:\test to C:\test.zip?

Much appreciated.
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Well I can send a mail no problem but the problem occurs with the zip code. I tried this from here:

'zipsample.vbs
'Sample VBScript using XStandard's "Zip Component"
'downloaded from and
'installed per instructions at the site.

'Task: Request a folder to be zipped, a Zip file
' name, then zip the folder's contents into
' the specified Zip file.

Option Explicit
Const cstrApp = "Sample Zip Script"
Dim strFolder, strZipFile, objZip


'strFolder = InputBox("Enter path to folder to zip", cstrApp)
'strZipFile = InputBox("Enter path and name of Zip file", cstrApp)
Set objZip = CreateObject("XStandard.Zip")



objZip.Pack strFolder, strZipFile

MsgBox "Zip operation complete", vbOKOnly, cstrApp
Set objZip = Nothing

and tried
Dim myObjFSO
Set myObjFSO = CreateObject("scripting.filesystemobject")
CheckFolder "C:\Dove"

Sub CheckFolder(strPath)
Dim objFolder
Dim objFile
Dim objSubdirs
Dim objLoopFolder

Set objFolder = myObjFSO.GetFolder(strPath)

'Do what you need to do below

For Each objFile In objFolder.Files
If UCase(Right(objFile.Name, 4)) = ".LOG" Then
Set MyObj=CreateObject("WScript.Shell")
Msgbox "C:\Dove\WinZIP.EXE test.zip " & strPath &"\"& objFile.Name
MyObj.Run "C:\Dove\WinIP.EXE test.zip " & strPath &"\"& objFile.Name

End If
Next


'Loop through all subdirectories and do the same thing (if required).

Set objSubdirs = objFolder.SubFolders
For Each objLoopFolder In objSubdirs
CheckFolder objLoopFolder.Path
Next

Set objSubdirs = Nothing
Set objFolder = Nothing

End Sub


MsgBox "Processed files.", vbSystemModal, "Batch zipper"

But neither work. I just want it to compress the files and appear on my C:. The rest I can possibly handle myself.
 
Ok, easiest is to first create a blank zip file and then use the copyhere command to copy the files into it. To create a blank zip use something like:


Set Ag=Wscript.Arguments
username = CreateObject("Wscript.Shell")_
.Environment("Process")("username")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso_OpenTextFile("c:\test1\"&UserName &" "&Date()&".zip", 8, 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

This creates an empty zip file in the directory c:\test1

After this you fill the zipfile. I'm still searching for the best wait command but you might want to let the script sleep for half a second to allow it to create the zip file before progressing.

Set objShell = CreateObject("Shell.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace("C:\test1\"&UserName&" "&Date()&".zip")
Set SrcFldr=objShell.NameSpace("c:\test2")
DestFldr.CopyHere ("c:\test2")

Now you've copied the contents of c:\test2 into the previously made zipfile...

Hope this helps. I'm just starting out myself and pieced this together from other posts. Check the MSDN website for a full description of the copyhere command.

regards,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top