2

I am trying to return a success/error code using Environment.Exit function in console application. (0 for success, 1 for error). Now i want to capture this return code in vbscript and display the return code (e.g. WScript.Echo(returnCode)).

Does anyone know if this can be done?

Thanks

1
  • the console app is return in C#. Aug 1, 2011 at 18:39

3 Answers 3

4

Launching other programs from a script.

exitcode = WshShell.Run(strCommand, , true])

If bWaitOnReturn is set to True, the Run method creates a new process, executes the command, and waits until the process terminates. In this case, the Run method returns the error code obtained from the terminated process. If bWaitOnReturn is missing or is set to False, Run returns the error code 0.

2
Set WshShell = WScript.CreateObject("WScript.Shell")
errcode =  WshShell.Run("your program.exe",,true)
1

Let's say your script you are running is called "Canada.vbs" and let's say the script that will return the code "Mexico.vbs".

CANADA.VBS Code (This is what you will run.):

Dim ExitCode
Dim Return
Dim WshShell
Set WshShell=CreateObject ("WScript.Shell")

Dim Command
Command="Mexico.vbs"
Command=Chr(34) & Command & Chr (34)
ExitCode=WshShell.Run (Command, 1, True)

If ExitCode="1" Then
Return="Success"
ElseIf ExitCode="2" Then
Return="Failure"
Else
Return="Unknown"
End If

MsgBox Return

MEXICO.VBS Code (This is what Canada will run.):

Dim Success

Success=MsgBox ("Do you want to Succeed?",vbQuestion + vbYesNo,"Your Goals are good?")

success=success-7        'Vbyes=6 vbno=7. True=-1 False=0.

'The Above Code is only for demonstration purposes. You may set success in your own statement

If Success Then          'Successful
Success=1
ElseIf not Success Then  'Failure
Success=2
Else                     'Unknown
Success=0
End If

WScript.Quit (Success)

Ok now in Canada you will need to declare a variable that will hold the exit code of Mexico. Let's name it
Dim ExitCode
We are also going to need a String Variable (Although you can double up the ExitCode variable as this but doing so could make debugging difficult with large files. so let's keep them seperate.)

Dim Return

and we are going to need an object that will run the Mexico.vbs. the object we need is the Wscript Shell

Dim WshShell
Set Wshshell=CreateObject ("WScript.Shell")

Now we need to Specify the Script to run. the script path being "Mexico.vbs"

Dim Command
Command="Mexico.vbs"

Now JUST IN CASE there are any spaces in the filepath (there isn't but let's be safe) we need to enclose the path in quotes. (Character code: 34)

Command=Chr(34) & Command & Chr (34)

Now we need to run the Mexico.vbs from the Canada.vbs and save the return to a variable

ExitCode=WshShell.Run (Command, 1, True) 'command is path, 1 is window state, true is wait for return.

Now we format the ExitCode into an UserFriendly string.

If ExitCode="1" Then
Return="Success"
ElseIf ExitCode="2" Then
Return="Failure"
Else
Return="Unknown"
End If

Now we will display the Result in a msgbox.

MsgBox Return

In Mexico.vbs you will be sending the success or failure through an exit code. We will name the exitcode variable Success

Dim Success

THIS PART IS FOR DEMONSTRATION PURPOSES. You can exclude this portion and replace it with your auditing statements. Just set the Success to true if successful or False if Failure.

Success=MsgBox ("Do you want to Succeed?",vbQuestion + vbYesNo,"Your Goals are good?")

success=success-7        'Vbyes=6 vbno=7. True=-1 False=0.

'The Above Code is only for demonstration purposes. You may set success in your own statement

Now we will convert the bool Success into an Integer, 1=success,2=failure,0=unknown

If Success Then          'Successful
Success=1
ElseIf not Success Then  'Failure
Success=2
Else                     'Unknown
Success=0
End If

And now we will terminate the Mexico.vbs Script with the exit code set in Success.

WScript.Quit (Success)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.