The VBScript Network and Systems Administrator's Cafe

Oct 17 2008   11:11PM GMT

Communicating error levels to a calling application from with in VBScript



Posted by: Jerry Lees
Tags:
batch commands
Error control
errorlevel
exit
on error
VBScript
wscript
WScript.quit

We’ve discussed error handling previously (via the On Error Goto 0 command) in a entry titled, Using error control in a VBScript to recover from odd errors. However, occasionally you need to communicate a condition back to an application or batch file that calls a script as part of its processing. One reason you may need to do this is if you need to call a separate script or application depending on the outcome of the script. You can do this with what is called an error level. Keep in mind this may not have anything to do with an error in the script but can, instead, be used as a method to control how another applicatioin responds to a particular condition.

For example, say you already have a executable that processes a file into a database– but you don’t want to run the executable on a old file because it would cause all sorts of duplicated entries in the database. you might not be able to go back to the person who wrote the executable and have them change it or the executable might not have even been written by the company where you work! You could, however write a script to check the date of a file and if it was created during a time frame return a error level back to a batch file that calls your script and preforms the logic to decide if the executable needed to be ran. Take this batch file for example:

Echo off
cscript Yourscript.vbs
If %errorlevel% == 3 goto runexe
Echo “File to old”
goto EndBatch
:runexe
processingprogram.exe
:EndBatch

This would only run the executable IF your script returned a error level of 3! So you can then preform work and checks never intended by the executable when it was written only if the conditions are correct for it to run.

The line of code in VBScript you would add to your  script to communicate back to the batch file would be:

WScript.quit(3)

This will return a error level of 3 back to the batch file and cause the batch file to THEN run your executable.

So, there you have it! A quick and simple method to extend the life of those aging processes that work fine– just need a minor tweak because your environment has changed.

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jefftanner
    I have tried the following, and I cannot get [B]%ERRORLEVEL%[/B] to be set any other value but zero(0) after using [B]WScript.Quit(3)[/B] within [B]VBScript[/B] and [B]JScript[/B] files. Here is my code. What I am doing wrong? [B]QuitTest.cmd[/B] [CODE] @ECHO OFF ECHO. cscript QuitTest.vbs //NoLogo ECHO QuitTest.vbs: %errorlevel% ECHO. cscript QuitTest.js //NoLogo ECHO QuitTest.js: %errorlevel% [/CODE] [B]QuitTest.js[/B] [CODE] WScript.Echo("JScript QuitTest"); WScript.Quit(3); [/CODE] [B]QuitTest.vbs[/B] [CODE] WScript.Echo("VBScript QuitTest") WScript.Quit(3) [/CODE] [B]RUN:[/B] [CODE] D:\Workspace>QuitTest.cmd VBScript QuitTest QuitTest.vbs: 0 JScript QuitTest QuitTest.js: 0 [/CODE] Thanks Jeff in Seattle
    0 pointsBadges:
    report
  • Jerry Lees
    Jeff, I didn't see this comment until just now. Terribly sorry for the delay in my response. I'm not sure what the issue may be, I quickly wrote the following and it works as specified. Have you gotten it figured out already? [b]Test.bat[/b] [CODE]Echo off cscript Yourscript.vbs If %errorlevel% == 3 goto runexe Echo “File to old” goto EndBatch :runexe Echo “Error level 3 achieved” :EndBatch [/CODE] [b]YourScript.Bat[/b] [CODE]wscript.quit(3)[/CODE] [b]run of test.bat from a command prompt[/b] >test >Echo off Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. Error level 3 achieved
    5,335 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: