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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically ZIP files within a folder

Status
Not open for further replies.

Paul318

Technical User
Aug 10, 2001
53
GB
Hi,
We have a folder structure folder1, folder2, folder3, etc and within these folders are files and subfolders. We would like to script .zip'ing of these files and foilders and name them the same as the parent foler so folder1.zip will sit in Folder1 and hold the contents of Folder1.
Can anyone help? We have a script which I will paste below but it doesnt seem to work, any help appreciated.
Thanks.
Paul
Function Zip(sFile,sArchiveName)
'This function executes the command line
'version of WinZip and reports whether
'the archive exists after WinZip exits.
'If it exists then it returns true. If
'not it returns an error message.

'This script is provided under the Creative Commons license located
'at . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("Wscript.Shell")

'--------Find Working Directory--------
aScriptFilename = Split(Wscript.ScriptFullName, "\")
sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
sWorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")
'--------------------------------------

'-------Ensure we can find WZZIP.exe------
If oFSO.FileExists(sWorkingDirectory & " " & "WZZIP.EXE") Then
sWinZipLocation = ""
ElseIf oFSO.FileExists("C:\program files\WinZip\WZZIP.EXE") Then
sWinZipLocation = "C:\program files\WinZip\"
Else
Zip = "Error: Couldn't find WZZIP.EXE"
Exit Function
End If
'--------------------------------------

oShell.Run """" & sWinZipLocation & "wzzip.exe"" -ex -r -p -whs -ybc """ & _
sArchiveName & """ """ & sFile & """", 0, True

If oFSO.FileExists(sArchiveName) Then
Zip = 1
Else
Zip = "Error: Archive Creation Failed."
End If
End Function
 
Are you using VBScript or trying to run this in VB6?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I'm not sure to be honest, I'm not too savvy with VB (clearly), I was told to save as zip.vbs and run it?
 
Ok, no worries. That's a VBScript file. [smile]

Have you verified that you have WZZIP.exe in either the same folder as your zip.vbs or in C:\program files\WinZip\?

Regards

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Actually, looking at it, I don't think you ever call the function you've got there.

Above the function in the file try:
Code:
Zip "PathToFileYouWantToZip","PathToArchiveYouWantToCreate"
Replace the values I've used with the values of the files you want to use.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks for your respnse HarleyQuinn, however I'm completely lost!
I think it's best we comission someone else to create the script!
But I appreciate your input, so many thanks.
 
Hi,

If you have Winzip installed on the machine and VB6 then try the following

Start a new VB6 project

Add the following to a module

Code:
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
      hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
      lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
      ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
      ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
      lpStartupInfo As STARTUPINFO, lpProcessInformation As _
      PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

Sub zipup(fileName As String, files As String)

'sub to zip a files using winzip - winzip will run in the backgroud
'pass the full path of the zip file to create and the full path
'of the files to zip

Const ZIP_EXE As String = """C:\Program " & _
    "Files\WinZip\winzip32"""

Dim commandLine As String
Dim retval As Long

commandLine = ZIP_EXE & " -a -r """ & fileName & """ """ & files & """"

retval = ExecCmd(commandLine, True)
    
End Sub

Public Function ExecCmd(cmdline$, hide As Boolean)
      Dim proc As PROCESS_INFORMATION
      Dim start As STARTUPINFO
      
      'We need to define the following as we are using 'C' style code
      'Normally these would be defined in the 'C' Header Windows.h
      Const STARTF_USESHOWWINDOW = &H1&
      Const NORMAL_PRIORITY_CLASS = &H20&


      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)
      If (hide) Then
        start.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
        start.wShowWindow = SW_HIDE
      End If

      ' Start the shelled application:
      ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)

      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
End Function
[\code]

Then on a new form add a command button and paste the following code to the on click

zipup [zip location], [file or folders to zip]

Cheers

Stuart
 
Of course, Windows (and VB6) can zip (with certain limitations) without any 3rd party tools ... I've certainly illustrated how to do so in this forum, so a keyword search ought to find something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top