How to check the exit code after WScript.Quit

Posted on 2006-04-06
Medium Priority
Last Modified: 2011-10-03
Hello everyone,

Here's basicly what my script looks like.

WScript.Echo("Hello", "world")
WScript.Quit 1
WScript.Echo("You should never see this.")

That "1" is what I believe to ba an exit code.

How do I check for its value ?

I have a .vbs script(two) that is called from another .vbs(one) script.
If .vbs script (two) fails,  I want to stop it with a specific exit code.

Then in the calling .vbs script(one), I want to evaluate the exit code value and display a specific message based it the value.

Any idea how I might evaluate the exit code ?

  • 3
  • 2
  • 2
LVL 20

Expert Comment

ID: 16396798
How are you calling the second script from the first
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16397903
Check out the ExitCode() property of the returned object from the Exec() method in the WScript.Shell class:
ID: 16400152
First,  thanks for both responses.

As to the question from hes:

In .vbs script one,  I have the following line of code which calls, or executes,  .vbs script two. (named GetPDBs.vbs)
WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True

At several places within .vbs script two(GetPDBs.vbs),  things could go wrong.

I am writing lines into a log file on the hard drive, as .vbs script two runs, so if any failures occur,  I know what the failures are and where, within .vbs script two,  they occurred.

My goal is,  to stop .vbs script two when the failure occurs AND .vbs script one as well.

I CAN successfully stop .vbs script two by coding "WScript.Quit 1".

But of course,  control is returned to .vbs script one,  and .vbs script one continues on its merry way.

MY thinking,  was to,  add some lines of code to .vbs script one,  right after the "WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True" line,  that could check for the exit code generated by the "WScript.Quit 1" line in .vbs script two.

Then,  if it were "1" or something other than "0",  execute another "WScript.Quit 1" which would bring .vbs script one to a halt.

Make sense ?

If there is another way to accomplish the same thing,  I'm open to,  and welcome,  any other suggestions.

Second,  to Idle Mind's comment:

Thanks for the link.

I'm new to vbs scripting.
WAY new.

I truly appreciate your efforts to help me,  but the information and examples are over my head.

I'm just plain, "not that smart yet",  to be able to read those example and apply anything from them to MY issue.

(Now you know why I need to be a member of this forum.)

I'll get there.
I'm just not there at this point in my scripting career.
Thanks again.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Accepted Solution

Mike Tomlinson earned 1600 total points
ID: 16400286
No problem...    =)

Here is a simple example:

' ----------------
' Script2.vbs
' ----------------
WScript.Echo "Hello world!"
WScript.Quit 1
WScript.Echo "You should never see this."

' ----------------
' Script1.vbs
' ----------------
Dim WshShell, oExec

' launch the second script via "CScript"...  (assuming Script2.vbs is in the same directory)
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("CScript Script2.vbs")

' wait for second script to exit...
Do While oExec.Status = 0
   WScript.Sleep 100

' do something based on the exit code...
Select Case oExec.ExitCode
    Case 0
        WScript.Echo "Continuing first script..."
        ' ...more code here...
    Case 1
        WScript.Echo "Second script had an Error!"
        WScript.Quit 1

    Case Else
        WScript.Echo "Something else happened..."
        ' ...more code here...

End Select
LVL 20

Assisted Solution

hes earned 400 total points
ID: 16400314
Return = WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True
Select Case Return
End Select

The True as the last parameter means
Wait for the command to complete before continuing execution of the wsh script.
ID: 16401112
I can't thank you folks enough.

Things are working perfectly now.

I NEVER(at least not for some time) would have thought of the CASE solution.

You folks truly have made my day.
My entire week end in fact.

So,  here's how I used the information.

In script number two,  after handling the error condition(i write an audit line into a text file on the hard drive),  I end the script with the following 'WScript.Quit (1)

Then,  in script number one (this calls script number two) I do the following which,  thanks to you good folks,  now ends script number one like I had hoped for.

Return = WshShell.Run ("cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True)

Select Case Return
  Case 1
End Select  


You folks are the greatest.

Thanks again and take care.

Ray in Wisconsin
LVL 20

Expert Comment

ID: 16401288
Glad to have helped

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question