Re: Return code and batch files...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Pegasus (MVP)" <I.can@xxxxxxxxxx> wrote in message
news:%23D4FPvuMJHA.5696@xxxxxxxxxxxxxxxxxxxxxxx

<thadfield@xxxxxxxxx> wrote in message
news:3241f310-b39c-4591-8cc4-5ee67805b648@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have a simple vbs code that has only the following line in it:
WScript.Quit WScript.Arguments(0)

And a batch file that does the following:

cscript test.vbs 0
echo first test = %errorlevel%
cscript test.vbs 99
echo second test = %errorlevel%
cscript test.vbs 0
echo third test = %errorlevel%

if I run the batch file I get the following expected results:
C:\temp>test.bat

C:\temp>cscript test.vbs 0
C:\temp>echo first test = 0
first test = 0

C:\temp>cscript test.vbs 99
C:\temp>echo second test = 99
second test = 99

C:\temp>cscript test.vbs 0
C:\temp>echo third test = 0
third test = 0

Now, if I run the following command before running test.bat the echo
of errorlevel always returns 0:
c:\temp>set errorlevel=0
c:\temp>test.bat

C:\temp>test.bat

C:\temp>cscript test.vbs 0
C:\temp>echo first test = 0
first test = 0

C:\temp>cscript test.vbs 99
C:\temp>echo second test = 0
second test = 0

C:\temp>cscript test.vbs 0
C:\temp>echo third test = 0
third test = 0

Do you know why if I type set errorlevel=0 that the batch file is not
working correctly? If I type set errorlevel= with nothing after it,
everything works as expected. Can anyone explain this behavior?

And finally... if I have a syntax error in test.vbs (like calling it
without a parameter) it returns 0 no matter what, is this correct?

By forcing errorlevel=0 you create your own environmental variable which
will hide the variable returned by your VB Script. Not a good idea. To
unhide it you must first issue this command:
set errorlevel=

About your VB Script syntax error: Use cscript.exe if you want to interact
with the Command Console, e.g. when using wscript.echo. The same probably
applies to the "quit" method.

the OP *is* using cscript.exe. The problem is that the error message flies
by so fast he does not see it. If the wscript.quit fails to exit because of
the error, then it exits (returning a zero errorlevel) because it has
reached the end of the script.

I modified the batch script to add a fourth test with no parameter - its
output is shown below:


C:\Documents and Settings\Al\Desktop\z>cscript test.vbs 0
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Documents and Settings\Al\Desktop\z>echo first test = 0
first test = 0

C:\Documents and Settings\Al\Desktop\z>cscript test.vbs 99
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Documents and Settings\Al\Desktop\z>echo second test = 99
second test = 99

C:\Documents and Settings\Al\Desktop\z>cscript test.vbs 0
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Documents and Settings\Al\Desktop\z>echo third test = 0
third test = 0

C:\Documents and Settings\Al\Desktop\z>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Documents and Settings\Al\Desktop\z\test.vbs(1, 1) Microsoft VBScript
runtime
error: Subscript out of range


C:\Documents and Settings\Al\Desktop\z>echo fourth test = 0
fourth test = 0

C:\Documents and Settings\Al\Desktop\z>pause
Press any key to continue . . .


/Al


.



Relevant Pages

  • Return code and batch files...
    ... echo first test = %errorlevel% ... echo second test = %errorlevel% ... echo third test = %errorlevel% ...
    (microsoft.public.scripting.wsh)
  • Re: Return code and batch files...
    ... echo first test = %errorlevel% ... echo second test = %errorlevel% ...
    (microsoft.public.scripting.wsh)
  • How to adapt two tests so they have the same difficulty level?
    ... I am a student of linguistics working on my thesis experiment and I ... have a problem analyzing my data. ... first test - test version A - condition X ... second test - test version B - condition Y ...
    (sci.stat.edu)
  • Re: Jaffers Strokeplay
    ... But after seeing the first test and second test, ... with his stylish / wristy stroke play. ... I feel that he is the most stylish player in this team. ...
    (rec.sport.cricket)