0

I want to zip a folder using batch file. Here's my code for zip.bat:

CScript zip.vbs E:\app E:\app.zip

zip.vbs has the following code:

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 2000000

This code is ziiping the folder properly but i dont want to mention any drive name. I want that in any drive if i keep my bat file n app folder then after running the bat file it should zip the folder. Is there any code for this???

Dimple
  • 111
  • 2
  • 11

1 Answers1

0

You'll need to add FileSystemObject and to use GetAbsolutePathName function - shell.application namespaces does not accept relative paths.

Though using sleep is bad idea - it will either make the script too slow or will will interrupt the zipping.The better idea is to count the items in the source and in the destination as the zipping is transactional and files count is a robust way ti check if the operation is done.

You can check also my zipjs.bat which also uses Shell.Application but everything is packed in a hybrid jscript\.bat script (it even will no create temp files).It is able to process relative paths ,and comparatively robust (one of the most used scripts I've written - so I had some feedback and needed to fix numerous of bugs :-) )

More info here: How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?

Set objArgs = WScript.Arguments
set fso=CreateObject("Scripting.FileSystemObject"):
InputFolder =  fso.GetAbsolutePathName(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 2000000
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187