5

I have a problem about compressing a directory or a file in a batch file. HOw can I do this, Can anybody help me about this?

Thank you

Bali C
  • 30,582
  • 35
  • 123
  • 152
eponymous
  • 2,090
  • 5
  • 23
  • 29

3 Answers3

3

There is a good solution to a similar question on a Post on SuperUser, I have copy pasted it below.

CScript zip.vbs C:\test3 C:\someArchive.zip

Where zip.vbs contains the following

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
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)

'Required!
wScript.Sleep 2000
Community
  • 1
  • 1
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • I created a bat file which include "CScript zip.vbs C:\Temp C:\someArchive.zip" and I created zip.vbs in same directory. Then, I run the bat file but, it didn't work that I want. Where is my mistake? C:\Temp is the directory which I want to compress. – eponymous Jun 19 '12 at 13:41
  • There are no error messages. Only I see that it don't compress the folder. Then I created a bat file which include the code that the approved one of this link-> http://superuser.com/questions/110991/can-you-zip-a-file-from-the-command-prompt-using-only-windows-built-in-capabili . It run as individual bat file but when I copy-paste it in another bat file (in if-else block) it doesn't run. There are no problem about if-else, I am sure. I couldn't understand the problem. – eponymous Jun 20 '12 at 07:56
  • There are an error messages about in my last situation -> C:\Windows\system32\_zipIt.vbs(9, 8) Microsoft VBScript compilation error: Invalid character – eponymous Jun 20 '12 at 08:46
0

You could use a 3rd party tool, I suggest 7-Zip, which has a command line version you can use in batch.

See here for a list of usable commands and examples.

Bali C
  • 30,582
  • 35
  • 123
  • 152
0

Here you can find two ways without using any external tools .

Better use the WSH/Jscript script that uses the Shell.Application object.

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187