Real'sHowTo AddThis Feed Button
Custom Search

Get an exit code from a vbsTag(s): WSH VBScript


A return code can take a numeric value from 0 to 255.

Script returning an exit code

[script.vbs]

exitCode = InputBox ( "Enter Exit Code (0 - 255)", "Script", "0")
If Not IsNumeric(exitCode) Then exitCode = 0
wscript.Quit(exitCode Mod 255)
Script to handle the return code
Set oWS = WScript.CreateObject("WScript.Shell")
returnCode = oWS.Run("wscript.exe script.vbs", 0, True)
MsgBox "Script2's Return Code: " & returnCode

From a DOS script (BAT or CMD), you check the ERRORLEVEL value.

As a convention, an ERRORLEVEL at 0 means a SUCCESS (255 is the maximum value). The DOS IF ERRORLEVEL construction has one strange feature, it returns TRUE if the return code is equal to or higher than the specified errorlevel. So you must check the highest possible value first.

[somescript.vbs]

' even with a return code of 2, the DOS IF ERRORLEVEL 1 will catch it
wscript.Quit(2)

@echo off
wscript somescript.vbs
echo wscript returned %errorlevel%
if errorlevel 1 goto error
echo We have success
goto end
:error
echo We have an error
:end

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]