# vbscript.  Any way to have a script wait for a shell command before continuing?

**URL:** <https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473>\
**Category:** Factual Questions\
**Created:** [March 7, 2008, 6:01pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473 "2008-03-07T18:01:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [March 7, 2008, 6:01pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/1 "2008-03-07T18:01:49Z")

</div>

I have a vbscript which runs a shell command to create a zip file and then send it via email.

Problem is the rest of the vbscript runs before the shell command has finished, and the vbscript therefore tries to send a file which doesn’t exist yet.

I solved this by adding a ten second delay, but this is far from ideal (what if the shell command takes longer than ten seconds in the future)

Is there a way to have the vbscript detect when the shell command has finished it’s job and then continue?

the shell command is as follows…

"oShell.Run "pkzip “&Zipfile&” “&FileName”

---

<div class="post-metadata">

**Author:** ![FordPrefect](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@FordPrefect](https://boards.straightdope.com/u/FordPrefect)\
**Post date:** [March 7, 2008, 7:01pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/2 "2008-03-07T19:01:44Z")

</div>

Can you create a polling loop that checks for the existence of the file before continuing?

---

<div class="post-metadata">

**Author:** ![3waygeek](https://avatars.discourse-cdn.com/v4/letter/3/e95f7d/32.png) [@3waygeek](https://boards.straightdope.com/u/3waygeek)\
**Post date:** [March 7, 2008, 7:09pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/3 "2008-03-07T19:09:34Z")

</div>

[QUOTE=Lobsang]  
I have a vbscript which runs a shell command to create a zip file and then send it via email.

Problem is the rest of the vbscript runs before the shell command has finished, and the vbscript therefore tries to send a file which doesn’t exist yet.

I solved this by adding a ten second delay, but this is far from ideal (what if the shell command takes longer than ten seconds in the future)

Is there a way to have the vbscript detect when the shell command has finished it’s job and then continue?

the shell command is as follows…

"oShell.Run "pkzip “&Zipfile&” “&FileName”  
[/QUOTE]

oShell is a WshShell object? If so, something like oShell.Run(“pkzip " &Zipfile&” “&FileName”, 0, true) should work. The 2nd argument to WshShell.Run indicates that pkzip executes in a hidden window; the 3rd argument tells the shell object to wait for the command to finish before returning (which is what you’re asking for).

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [March 7, 2008, 11:04pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/4 "2008-03-07T23:04:06Z")

</div>

**FordPerfect** Good idea that, I would have tried it, but 3WayGeek’s advice worked too so I went with that.  
Thanks for the help guys 🙂

---

<div class="post-metadata">

**Author:** ![Mangetout](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mangetout/32/19_2.png) [@Mangetout](https://boards.straightdope.com/u/Mangetout)\
**Post date:** [March 7, 2008, 11:24pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/5 "2008-03-07T23:24:09Z")

</div>

The way I dealt with this in the past is have VB(not VBScript, but the principle is the same) execute a batch file, containing the Zip instruction, plus a second command to create a text file called done.txt - then have VB wait until it could find done.txt, before proceeding. It was dirty, but it did work.

---

<div class="post-metadata">

**Author:** ![3waygeek](https://avatars.discourse-cdn.com/v4/letter/3/e95f7d/32.png) [@3waygeek](https://boards.straightdope.com/u/3waygeek)\
**Post date:** [March 8, 2008, 1:52am UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/6 "2008-03-08T01:52:56Z")

</div>

[QUOTE=Lobsang]  
3WayGeek’s advice worked too so I went with that.  
[/QUOTE]

Glad to hear it – I’ve written maybe 2 vbscripts in my life (I’m a C++/C/Assembler guy), but I know my way around the MSDN library.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [March 8, 2008, 12:30pm UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/7 "2008-03-08T12:30:12Z")

</div>

It’s amazing how often the solution to a seemingly hard problem turns out to be an overload to a well known function that we just don’t use very often.

---

<div class="post-metadata">

**Author:** ![FordPrefect](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@FordPrefect](https://boards.straightdope.com/u/FordPrefect)\
**Post date:** [March 9, 2008, 12:20am UTC](https://boards.straightdope.com/t/vbscript-any-way-to-have-a-script-wait-for-a-shell-command-before-continuing/440473/8 "2008-03-09T00:20:43Z")

</div>

[QUOTE=Lobsang]  
**FordPerfect** Good idea that, I would have tried it, but 3WayGeek’s advice worked too so I went with that.  
Thanks for the help guys 🙂  
[/QUOTE]

No worries, I had no idea that there was an overload like that. **3WayGeek** had the better solution. Mine was just a hack.

[QUOTE=LSLGuy]  
It’s amazing how often the solution to a seemingly hard problem turns out to be an overload to a well known function that we just don’t use very often.  
[/QUOTE]

Amen brother, amen.
