|Title||Let the user select and Zip files individually|
|Description||This example shows how to let the user select and Zip files individually in Visual Basic 6. The program uses DriveListBox, DirListBox, and FileListBox controls to let the user select files. It then iterates over those files using WinZip's command line interface to Zip them up.|
|Keywords||Zip, WinZip, files, compress, archive|
|Categories||Utilities, Files and Directories|
The program uses DriveListBox, DirListBox, and FileListBox controls to let the user select files. It then iterates over those files using WinZip's command line interface to Zip them up.
Note that the file names are probably long names so the program encloses them in quotes.
' Zip the selected files.
Private Sub mnuFileZip_Click()
Const QUOTE As String = """"
Const ZIP_EXE As String = QUOTE & _
"C:\Program Files\WinZip\wzzip" & QUOTE
Dim dir_name As String
Dim file_name As String
Dim zip_name As String
Dim pos As Integer
Dim cmd As String
Dim i As Integer
' Get the selected directory name.
dir_name = DirList.Path
If Right$(dir_name, 1) <> "\" Then dir_name = dir_name _
' Process the selected files.
For i = 0 To FileList.ListCount - 1
If FileList.Selected(i) Then
' Get the file and Zip file names.
file_name = dir_name & FileList.List(i)
pos = InStrRev(file_name, ".")
zip_name = Left$(file_name, pos) & "zip"
file_name = QUOTE & file_name & QUOTE
zip_name = QUOTE & zip_name & QUOTE
' Zip this file.
cmd = ZIP_EXE & " " & zip_name & " " & file_name
If Shell(cmd, vbHide) = 0 Then
If MsgBox("Zip Failed for " & file_name & _
vbCrLf & "Continue?", _
vbExclamation Or vbYesNo, _
"Zip Error") = vbNo _
Apparently you can only shell WinZip in vbHide mode if you have licensed WinZip. If you are using the evaluation version, you need to run the shell in vbNormalFocus mode. Then WinZip displays a message saying it is running in evaluation mode and you must press a key to continue.
Thanks to Pooya Kashefifar for pointing this out!
WinZip is such a worthwhile program that I encourage you to register.