Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VB Script for creating zip file and copying files into it.

2,608 views
Skip to first unread message

Chitra Balasubramani

unread,
Nov 28, 2016, 1:58:01 AM11/28/16
to
Hello,

I'm trying to create a zip file and copy files into it via VBS code.


Problem is Files are not getting copied, only zip file is getting created.



Here is my code :

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("C:\apace-jmeter-3\bin\Results\outcome.zip", 2, True)
ts.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
Set fso = nothing
Set ts = nothing

WScript.Sleep 5000


Set objShell = CreateObject("Shell.Application")
Set DestFldr=objShell.NameSpace("C:\apace-jmeter-3\bin\Results\outcome.zip")
Set SrcFldr=objShell.NameSpace("C:\Users\chitra.balasubramani\Desktop\Mail")
DestFldr.CopyHere SrcFldr

Thanks in advance.

R.Wieser

unread,
Nov 28, 2016, 3:22:03 AM11/28/16
to
Chitra,

> Problem is Files are not getting copied, only zip file is getting created.

Try ading a "sleep" after the CopyHere command, and see what happens.

Explanation: When the CopyHere has been requested, but the actual copying
hasn't started yet (it takes a bit of time to gather all the neccesary file
info) the ending of the script closes the ZIP object, not giving it a chance
to actually start the copying (when the process is started the ZIP object
will not close until it has finished copying)

Regards,
Rudy Wieser


-- Origional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
272535d3-23c0-44b7...@googlegroups.com...

Chitra Balasubramani

unread,
Nov 28, 2016, 5:25:43 AM11/28/16
to
Wieser,

Thanks for the response.

Tried adding sleep. Now getting "Access Denied Error"


ZipFile = "C:\apace-jmeter-3\bin\Results\outcome.zip"
SourceFolder ="C:\Users\chitra.balasubramani\Desktop\Mail"

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile(ZipFile, True)
ts.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
ts.Close
Set ts = Nothing
Set fso = Nothing

Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)

' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items

' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 5000 'Wait for items to be copied

Thanks,
Chitra

R.Wieser

unread,
Nov 28, 2016, 5:45:31 AM11/28/16
to
Chitra,

> Tried adding sleep. Now getting "Access Denied Error"

That seems to mean that the CopyHere is actually trying to copy some
file(s). :-)

I get the feeling that you get that error because one of the files in
"C:\Users\chitra.balasubramani\Desktop\Mail" is still in use (is your mail
program still running ? If so, close it before running the script).

Have you tried to just copy those files to a temporary normal folder and see
if you get the same error (using VBScript but also try drag-and-dropping
them using the file/windows explorer) ?

Regards,
Rudy Wieser


-- Origional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
6b6acec6-bf5a-401b...@googlegroups.com...

Chitra Balasubramani

unread,
Nov 28, 2016, 5:47:50 AM11/28/16
to
I tired copying to normal folder, instead of zip. That worked fine.

R.Wieser

unread,
Nov 28, 2016, 6:19:06 AM11/28/16
to
Chitra,

> I tired copying to normal folder, instead of zip. That worked fine.

Hmmm ...

There is one more thing you can try: Try changing

zip.CopyHere(sourceItems)

to

zip.CopyHere(sourceItems.items)

(the former will copy the foldername too, the latter only the files in that
folder)

If that does not work you could choose to first copy the needed files to a
temporay folder (which you could create and remove in the script itself),
and from there copy them into the ZIP file. Yes, I know that that is a
work-around, but if the direct way fails you need to come up with a fix. :-)

Regards,
Rudy Wieser


-- Origional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
74a5c05c-e698-4286...@googlegroups.com...
> On Monday, 28 November 2016 16:15:31 UTC+5:30, R.Wieser wrote:
> > Chitra,
> >
> > > Tried adding sleep. Now getting "Access Denied Error"
> >
> > That seems to mean that the CopyHere is actually trying to copy some
> > file(s). :-)
> >
> > I get the feeling that you get that error because one of the files in
> > "C:\Users\chitra.balasubramani\Desktop\Mail" is still in use (is your
mail
> > program still running ? If so, close it before running the script).
> >
> > Have you tried to just copy those files to a temporary normal folder and
see
> > if you get the same error (using VBScript but also try drag-and-dropping
> > them using the file/windows explorer) ?
> >
> > Regards,
> > Rudy Wieser
> >
...

Chitra Balasubramani

unread,
Nov 28, 2016, 7:07:07 AM11/28/16
to
Wieser,

If the dest folder is "Set DestFldr=objShell.NameSpace("C:\apace-jmeter-3\bin\Results\Temp")", Copy is working.

If the dest is Set DestFldr=objShell.NameSpace("C:\apace-jmeter-3\bin\Results\outcome.zip")", getting access denied error.

Changed copyHere as "DestFldr.CopyHere (SrcFldr.Items)"

Thanks,
CHitra

R.Wieser

unread,
Nov 28, 2016, 11:02:13 AM11/28/16
to
Chitra (is that your first name ? I'm not sure),

> If the dest folder is "Set DestFldr=objShell.NameSpace(
> "C:\apace-jmeter-3\bin\Results\Temp")", Copy is working.
>
> If the dest is Set DestFldr=objShell.NameSpace(
> "C:\apace-jmeter-3\bin\Results\outcome.zip")", getting access
> denied error.

Hmmm ... I assumed the sourcefile(s) could be unreadable (because some
other program still has (one of) them open, but its possible that the target
file (the .ZIP) is the actual culprit here.

Have you already tried to open the created (but presumably empty) .ZIP file
? Can you ? If so, whats inside (folders, files)

Your code create the empty ZIP file like this:

ts.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )

could you try it like this:

ts.Write chr(80) & chr(75) & Chr(5) & Chr(6) & String( 18, 0)

Regards,
Rudy Wieser


-- Origional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
0bf6b357-ef5d-4242...@googlegroups.com...
> On Monday, 28 November 2016 16:49:06 UTC+5:30, R.Wieser wrote:
> > Chitra,
> >
> > > I tired copying to normal folder, instead of zip. That worked fine.
> >
> > Hmmm ...
> >
> > There is one more thing you can try: Try changing
> >
> > zip.CopyHere(sourceItems)
> >
> > to
> >
> > zip.CopyHere(sourceItems.items)
> >
> > (the former will copy the foldername too, the latter only the files in
that
> > folder)
> >
> > If that does not work you could choose to first copy the needed files to
a
> > temporay folder (which you could create and remove in the script
itself),
> > and from there copy them into the ZIP file. Yes, I know that that is a
> > work-around, but if the direct way fails you need to come up with a fix.
:-)
> >
> > Regards,
> > Rudy Wieser
> >
> >
...

Chitra Balasubramani

unread,
Nov 28, 2016, 11:06:16 PM11/28/16
to
Yes Chitra is my first name :)

Have tried below options :
1. Copying those files to folder instead of zip. - Worked
2. Copying files to empty zip which was created manually - Access Denied error.

So the problem is VBS is not able to copy files to any of the zip files.

And also if manually copy files to the same empty zip file, it is allowing.

And answer to your question, after running script, Zip is empty.


Evertjan.

unread,
Nov 29, 2016, 2:15:58 AM11/29/16
to
Chitra Balasubramani <chitra...@gmail.com> wrote on 29 Nov 2016 in
microsoft.public.scripting.vbscript:

> So the problem is VBS is not able to copy files to any of the zip files.
>
> And also if manually copy files to the same empty zip file, it is
> allowing.
>
> And answer to your question, after running script, Zip is empty.

I would think you cannot "copy" a file to a zip-file,
as it involves compression not available to the basic operating system,
and a zip-dile is not [just] a folder/directory.

Is this only a Q of semantics?

Your ordeal suggests otherwise.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

R.Wieser

unread,
Nov 29, 2016, 2:25:25 AM11/29/16
to
Chitra,

> Yes Chitra is my first name :)

:-) I was asking because you adressed me by my sirname, making me wonder if
I was talking to someone in a region where the persons own name is written
last, and the sirname first.

> Have tried below options :
> 1. Copying those files to folder instead of zip. - Worked
> 2. Copying files to empty zip which was created manually - Access Denied
error.

That seems to indicate that the sourcefiles are not the problem.

> And also if manually copy files to the same empty zip file, it is
allowing.
>
> And answer to your question, after running script, Zip is empty.

That means that the (empty) ZIP file itself is of the right structure.

Could you test what happens when you try to CopyHere to a .ZIP file you
created manually (not by script) ?

If that does *not* work than I am out of ideas I'm afraid.

If it works its possible that the .ZIP file is not actually closed before
you try to CopyHere to it. In that case putting a "sleep" between ... Hold
the presses!

I just took another look at the code you posted, and only now noticed
something I should have much earlier: where do you *close* the .ZIP file
after you've created it ?

Try adding "ts.close" just below the "Set ts = fso.OpenTextFile(...)" line
and see what happens.

Explanation: If the file is still open when the CopyHere command tries to
access it (trying to open it itself) it won't work.

Hope that helps,

Regards,
Rudy Wieser


-- Origoional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
fa9fa6c3-3da5-433a...@googlegroups.com...

Evertjan.

unread,
Nov 29, 2016, 2:47:07 AM11/29/16
to
"R.Wieser" <add...@not.available> wrote on 29 Nov 2016 in
microsoft.public.scripting.vbscript:

> :-) I was asking because you adressed me by my sirname, making me wonder if
> I was talking to someone in a region where the persons own name is written
> last, and the sirname first.

"Sir Rudy"?

R.Wieser

unread,
Nov 29, 2016, 2:56:32 AM11/29/16
to
Evertjan,

> "Sir Rudy"?

Finally, someone who acknowledges my grandness ! :-D

No, Chitra started his messages by adressing me by just my last name (no
"mr" prefix or alike). Which made me wonder if I was maybe (mistakingly!)
doing the same (adressing him by just his last name) because of a possible
different ordering of (what westerlings call) the first and last/sirname.

Regards,
Rudy Wieser


-- Origional message:
Evertjan. <exxjxw.h...@inter.nl.net> schreef in berichtnieuws
XnsA6CF5961...@194.109.6.166...

Chitra Balasubramani

unread,
Nov 29, 2016, 4:37:50 AM11/29/16
to
Rudy,

Yes the problem is "CopyHere" failing for the empty zip created manually.

Thanks,
Chitra

Chitra Balasubramani

unread,
Nov 29, 2016, 5:36:37 AM11/29/16
to
On Tuesday, 29 November 2016 12:45:58 UTC+5:30, Evertjan. wrote:
> Chitra Balasubramani <chitra...@gmail.com> wrote on 29 Nov 2016 in
> microsoft.public.scripting.vbscript:
>
> > So the problem is VBS is not able to copy files to any of the zip files.
> >
> > And also if manually copy files to the same empty zip file, it is
> > allowing.
> >
> > And answer to your question, after running script, Zip is empty.
>
> I would think you cannot "copy" a file to a zip-file,
> as it involves compression not available to the basic operating system,
> and a zip-dile is not [just] a folder/directory.
>
Yes that's what happening. Able to copy to normal folder not zip files.

R.Wieser

unread,
Nov 29, 2016, 5:55:49 AM11/29/16
to
Chitra,

> Yes the problem is "CopyHere" failing for the empty zip created manually.

I hope you remembered to disable the (re-)creating of the .ZIP file when you
did that (also, add the "close" command. It may not help, but it won't hurt
either) ?

As a (very) last test, what happens when you try to CopyHere a simple
textfile into the (manually) created .ZIP file ?

If that fails I'm really outof ideas I'm afraid.

.. Nope, one more: try changing.

Set ts = fso.OpenTextFile("C:\apace-jmeter-3\bin\Results\outcome.zip", 2,
True)

to

Set ts = fso.CreateTextFile("C:\apace-jmeter-3\bin\Results\outcome.zip")


I just did something I should have done long before: I ran your script on my
XPsp3 machine. And after adding a "Sleep" at the end it copied a folder to
the created .ZIP file without a problem. And that means I am *really*
outof ideas.

I almost get the feeling as if something else is interfeering with the
copying process. Like an AV product perhaps ...

Regards,
Rudy Wieser



Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
c7544106-b88c-4e7b...@googlegroups.com...
...

Dave "Crash" Dummy

unread,
Nov 29, 2016, 11:56:13 AM11/29/16
to
I cheat. Instead of trying to use a script created zip file, I use the
freeware 7-Zip utility. Life is so much easier.

Set WshShell = CreateObject("WScript.Shell")
cmd="D:\path\7z.exe a"
DestFldr=" C:\apace-jmeter-3\bin\Results\outcome.zip"
SrcFldr=" C:\Users\chitra.balasubramani\Desktop\Mail\*.*"
ret=WshShell.Run(cmd & DestFldr & SrcFldr,0,true)

--
Crash

"Never underestimate the power of the Dark Side."
~ Obi-Wan Kenobi ~

Chitra Balasubramani

unread,
Nov 29, 2016, 10:49:50 PM11/29/16
to
I tried the below code as well. Getting Access denied error.

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile("C:\apace-jmeter-3\bin\Results\outcome.zip", True)
ts.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
WScript.Sleep 5000
ts.Close
Set fso = nothing
Set ts = nothing
WScript.Sleep 5000

Set objShell = CreateObject("Shell.Application")
Set DestFldr=objShell.NameSpace("C:\apache-jmeter-3.02\bin\Results\temp.zip")
Set SrcFldr=objShell.NameSpace("C:\Users\chitra.balasubramani\Desktop\Mail")
DestFldr.CopyHere (SrcFldr.Items)
WScript.Sleep 5000

Thanks,
Chitra

R.Wieser

unread,
Nov 30, 2016, 2:50:41 AM11/30/16
to
Chitra,

> I tried the below code as well.
[snip]

I'm sorry, but I'm really outof ideas to try. And as I can't reproduce the
problem here, there is little I can do to find out why its not working at
your side.

I hope someone else has some insight to what the problem might be.

Regards,
Rudy Wieser


-- Origional message:
Chitra Balasubramani <chitra...@gmail.com> schreef in berichtnieuws
8342c826-ae94-40d9...@googlegroups.com...

Ulrich Möller

unread,
Nov 30, 2016, 7:52:23 AM11/30/16
to
Am 30.11.2016 um 04:49 schrieb Chitra Balasubramani:
> I tried the below code as well. Getting Access denied error.
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set ts = fso.CreateTextFile("C:\apace-jmeter-3\bin\Results\outcome.zip", True)
> ts.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
> WScript.Sleep 5000
> ts.Close
> Set fso = nothing
> Set ts = nothing
> WScript.Sleep 5000
>
> Set objShell = CreateObject("Shell.Application")
> Set DestFldr=objShell.NameSpace("C:\apache-jmeter-3.02\bin\Results\temp.zip")
> Set SrcFldr=objShell.NameSpace("C:\Users\chitra.balasubramani\Desktop\Mail")
> DestFldr.CopyHere (SrcFldr.Items)
> WScript.Sleep 5000
Hi,

may be you do have some access errors! Check each component of the
destination and source path if you have the correct rights (including
the newly created zipfile!) with the window command cacls or icalcs.

Here are some links:
http://www.robvanderwoude.com/vbstech_files_zip.php
http://www.xstandard.com/en/documentation/xzip/

Ulrich

Gloops

unread,
Dec 19, 2016, 7:40:35 AM12/19/16
to
Hello,

You can find an answer here :
http://stahlworks.com/dev/?tool=zipunzip

It is the same idea as 7z that Dave "Crash" Dummy points out, but with a
concurrent product, also freely downloadable.


Gloops

unread,
Dec 19, 2016, 7:42:36 AM12/19/16
to
Le 19/12/2016 à 13:40, Gloops a écrit :
> You can find an answer here :
> http://stahlworks.com/dev/?tool=zipunzip

My search argument was "zip command line".


0 new messages