Created On:  15 November 2010

Problem:

I am trying to test the return code from a COBOL program using errorlevel but I get the wrong value all the time. The program is using Goback using xxx where xxx = 8 but the value in errorlevel is always 0. 

The script I am using is :

@echo off
set errorlevel=0
testtest.exe
echo errorlevel = %ERRORLEVEL%

Resolution:

The problem here is in line 2, "set errorlevel=0", by doing this your are creating an environment variable called errorlevel and setting it to the value of 0.

Line 3 then executes the program called testtest.exe which returns a value of 8 on the GOBACK statement. This sets a special register called "errorlevel" and does not update the enviroment variable.

Line 4 then displays the value of %errorlevel% which is the value of the environment variable and not the special register setting.

To correct this remove line 2, which sets errorlevel to 0 and then %errorlevel% will be set to the special register value.
Incident #2479518