Solved

Batch Zip a file from command line.

Posted on 2013-01-16
15
2,409 Views
Last Modified: 2013-01-28
Hello Everyone,

I've always wondered if it is possible to compress a file from a batch in Windows.
What I want to do is compress 12 text documents in a zip from a batch file.
I found this solution but I'm not sure if it will work or not.
CScript  zip.vbs  C:\Folder  C:\Archive.zip
Where file zip.vbs contains:
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 2000

Open in new window

Any suggestion, help, will be very appreciate, thanks in advance.
0
Comment
Question by:Samuel-AMC
  • 8
  • 4
  • 2
15 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 38785782
There are many ways to do this:
7-zip has a command-line version
WinZip has a command-line version
Cygwin (unix ports) of zip can do this..

Even you can use Windows Natively from a command-line to do this..

However, I think this is what you are looking for..

http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereZIP

HTH,

Kent
0
 

Author Comment

by:Samuel-AMC
ID: 38785786
Hi kdyer, I would like an option like the "Windows Natively" you mentioned.
Why? in case the computer I want to use don't have no zipping programs like
the one you talked about.
0
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 38785788
See my amendment to include the link to Rob Vanderwoude that includes what you are looking for..

Sorry for the confusion.

Thanks,

Kent
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Samuel-AMC
ID: 38785814
Honestly I do not know what you referred to...
Maybe a link where I could get that info from?
0
 

Author Comment

by:Samuel-AMC
ID: 38785844
Oh! mybad  kdyer, I did not see that, I'm checking that out right now.
But here is the thing, I have 12 text documents files waiting to be zip,
how could I accomplish this task from a batch file or a vbs file? thanks
0
 

Author Comment

by:Samuel-AMC
ID: 38785898
I'm working with this code for now, but sometimes it zip the folder with only 8
text documents, sometimes with 10 or 11, etc. but not with the complete set of 12.
And I'm also getting this error message:
[Window Title]
Compressed (zipped) Folders Error

[Content]
File not found or no read permission.

[OK]

Open in new window


Option Explicit

Dim arrResult

arrResult = ZipFolder("C:\Folder-With-Files", "C:\MyZip.zip")
If arrResult(0) = 0 Then
If arrResult(1) = 1 Then
WScript.Echo "Done; 1 empty subfolder was skipped."
Else
WScript.Echo "Done; " & arrResult(1) & " empty subfolders were skipped."
End If
Else
WScript.Echo "ERROR " & Join(arrResult, vbCrLf)
End If

Function ZipFolder(myFolder, myZipFile)
Dim intSkipped, intSrcItems
Dim objApp, objFolder, objFSO, objItem, objTxt
Dim strSkipped

Const ForWriting = 2

intSkipped = 0

'Make sure the path ends with a backslash
If Right(myFolder, 1) <> "\" Then
myFolder = myFolder & "\"
End If

'Use custom error handling
On Error Resume Next

'Create an empty ZIP file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxt = objFSO.OpenTextFile(myZipFile, ForWriting, True)
objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
objTxt.Close
Set objTxt = Nothing

'Abort on errors
If Err Then
ZipFolder = Array(Err.Number, Err.Source, Err.Description)
Err.Clear
On Error GoTo 0
Exit Function
End If

'Create a Shell object
Set objApp = CreateObject("Shell.Application")

'Copy the files to the compressed folder
For Each objItem In objApp.NameSpace(myFolder).Items
If objItem.IsFolder Then
'Check if the subfolder is empty, and if
'so, skip it to prevent an error message
Set objFolder = objFSO.GetFolder(objItem.Path)
If objFolder.Files.Count + objFolder.SubFolders.Count = 0 Then
intSkipped = intSkipped + 1
Else
objApp.NameSpace(myZipFile).CopyHere objItem
End If
Else
objApp.NameSpace(myZipFile).CopyHere objItem
End If
Next

Set objFolder = Nothing
Set objFSO = Nothing

'Abort on errors
If Err Then
ZipFolder = Array(Err.Number, Err.Source, Err.Description)
Set objApp = Nothing
Err.Clear
On Error GoTo 0
Exit Function
End If

'Keep script waiting until compression is done
intSrcItems = objApp.NameSpace(myFolder).Items.Count
Do Until objApp.NameSpace(myZipFile).Items.Count + intSkipped = intSrcItems
WScript.Sleep 200
Loop
Set objApp = Nothing

'Abort on errors
If Err Then
ZipFolder = Array(Err.Number, Err.Source, Err.Description)
Err.Clear
On Error GoTo 0
Exit Function
End If

'Restore default error handling
On Error GoTo 0

'Return message if empty subfolders were skipped
If intSkipped = 0 Then
strSkipped = ""
Else
strSkipped = "skipped empty subfolders"
End If

'Return code 0 (no error occurred)
ZipFolder = Array(0, intSkipped, strSkipped)
End Function

Open in new window

0
 

Author Comment

by:Samuel-AMC
ID: 38792019
Hello again kdyer, I think this method is not too good as it does not compress all
the files in the folder, I wonder if you could tell me a better way even if I have to
do it with 7Zip or any other free compressor, and then use their command lines.
That would be nice if you could help me on that, thanks in advance.
But again I want to be able to run the command lines from a batch.
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 38797718
The solution that I use is for WinZip. It is not free, but it provides a simple command ine add-on (WZCLINE) that works great. You can call it from a batch file, vbscript, or other type of script very easily. You can find it at www.winzip.com/prodpagecl.htm.
0
 

Author Comment

by:Samuel-AMC
ID: 38798240
Hi BillBach, but where are the commands? I don't see any in the page you posted.

NOTE:
"WinZip Command Line, please make sure you have WinZip 12.1 or later installed."
This mean is not for any version of WinZip, which makes it useless for old versions.
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 38798923
Old versions of WinZip?  v11 was released in 2006, or 7 years ago.  How old do you want to go?

The CLI is pretty straightforward, as the included documentation shows.  Just use the WZZIP command and provide the zip filename and the files you want to zip up.  In a batch file, this would simply be:
   "C:\Program Files (x86)\WinZip\WZZIP.EXE"  ZipFile.ZIP  *.TXT
Or, if there were more than the 12 docs you wanted in the directory and you only wanted the given 12 files, use 12 lines, each adding one file, like this:
   "C:\Program Files (x86)\WinZip\WZZIP.EXE"  ZipFile.ZIP  File1.TXT
   "C:\Program Files (x86)\WinZip\WZZIP.EXE"  ZipFile.ZIP  File2.TXT
    ...
   "C:\Program Files (x86)\WinZip\WZZIP.EXE"  ZipFile.ZIP  File12.TXT
0
 

Author Comment

by:Samuel-AMC
ID: 38799472
"Old versions of WinZip?  v11 was released in 2006, or 7 years ago.
How old do you want to go?"

Ha-Ha-Ha-Ha-Ha !!! ^_^ Ha-Ha-Ha-Ha !!! Sorry Bill, but I could not help laughing :-)
Actually what I tried to said, or what I meant to said was WinZip 8.1 which is one
of the version that I've been using for a while.
0
 
LVL 28

Accepted Solution

by:
Bill Bach earned 500 total points
ID: 38799793
For those who are REALLY old school, you could probably find a copy of the DOS-based PKZIP.  The command line looks the same, but using PKZIP instead of WZZIP.
0
 

Author Comment

by:Samuel-AMC
ID: 38826233
Thanks Bill, I will have to accept this question for now, but this didn't work for me.
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 38827085
What part of it didn't work?  Command-line or batch file scripting is fairly straightforward.  Here's  a batch file that I use myself whenever I rebuild my own tools:

CALL :COPYIT AnyUsers
CALL :COPYIT BtrvInfo
CALL :COPYIT BtrvLoad
REM I took out a bunch of other tools...
CALL :COPYIT ViewUNF
GOTO :EOF

:COPYIT %1
"c:\program files (x86)\winzip\wzzip.exe" -a %1.ZIP %1\%1.PDF %1\Release\%1.EXE \develop\win32\SoftwareLicenseAgreement.PDF
EXIT /B

:EOF

Open in new window


This works perfectly. Whenever I want to build the ZIP files, I simply double-click the batch file -- and it runs.  I am using WinZip 17 with the command line component installed.

In fact, it is modeled directly after the batch files I used to use with the DOS-based PKZIP tool -- which won't run on Win64 boxes any more, but which still runs great on Win32 computers...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
The viewer will learn how to dynamically set the form action using jQuery.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question