Solved

Batch Zip a file from command line.

Posted on 2013-01-16
15
2,517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Introduction to Processes

691 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