Solved

Batch Zip a file from command line.

Posted on 2013-01-16
15
2,374 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
Comment Utility
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
Comment Utility
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
Comment Utility
See my amendment to include the link to Rob Vanderwoude that includes what you are looking for..

Sorry for the confusion.

Thanks,

Kent
0
 

Author Comment

by:Samuel-AMC
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 28

Expert Comment

by:Bill Bach
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now