Samuel-AMC
asked on
Batch Zip a file from command line.
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.
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
Any suggestion, help, will be very appreciate, thanks in advance.
ASKER
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.
Why? in case the computer I want to use don't have no zipping programs like
the one you talked about.
See my amendment to include the link to Rob Vanderwoude that includes what you are looking for..
Sorry for the confusion.
Thanks,
Kent
Sorry for the confusion.
Thanks,
Kent
ASKER
Honestly I do not know what you referred to...
Maybe a link where I could get that info from?
Maybe a link where I could get that info from?
ASKER
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
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
ASKER
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:
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]
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
ASKER
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.
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.
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.
ASKER
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.
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.
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
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
ASKER
"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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Bill, I will have to accept this question for now, but this didn't work for me.
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:
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...
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
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...
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