Wrong errorlevel value expected

  • Thread starter Thread starter jeeji
  • Start date Start date
J

jeeji

Hi

I have a .Net 3.5 windows console application (TextTool.exe) that
makes a call to Environment.Exit(1), which AKAIK sets the errorlevel
to 1. But this does not work as expected.

When I call it from a command line script and check for errorlevel 0,
I get True.
So the following script:

TextTool.exe
if errorlevel 0 (
echo Exited with 0
)


always echoes "Exited with 0", eventhough it makes a call to
Environment.Exit(1).
If I in between do an echo %errorlevel% I get 1. So the following
script

TextTool.exe
echo Errorlevel is %errorlevel%
if errorlevel 0 (
echo Exited with 0
)

always echoes:
Errorlevel is 1
Exited with 0

If I instead perform the check:
TextTool.exe
if %errorlevel%==0 (
echo Exited with 0
)

it works fine.
HOW COME???

Thanks
Jeeji
 
Hi

I have a .Net 3.5 windows console application (TextTool.exe) that
makes a call to Environment.Exit(1), which AKAIK sets theerrorlevel
to 1. But this does not work as expected.

When I call it from a command line script and check forerrorlevel0,
I get True.
So the following script:

TextTool.exe
iferrorlevel0 (
  echo Exited with 0
)

always echoes "Exited with 0", eventhough it makes a call to
Environment.Exit(1).
If I in between do an echo %errorlevel% I get 1. So the following
script

TextTool.exe
echoErrorlevelis %errorlevel%
iferrorlevel0 (
  echo Exited with 0
)
OK
No need to answer. I just realized that the statement
"if errorlevel 0" is true if errorlevel >=0, which is always the case.

So it makes more sense to test for "if errorlevel 1"

Jeeji
 
jeeji said:
Hi

I have a .Net 3.5 windows console application (TextTool.exe) that
makes a call to Environment.Exit(1), which AKAIK sets the errorlevel
to 1. But this does not work as expected.

When I call it from a command line script and check for errorlevel 0,
I get True.
So the following script:

TextTool.exe
if errorlevel 0 (
echo Exited with 0
)


always echoes "Exited with 0", eventhough it makes a call to
Environment.Exit(1).
If I in between do an echo %errorlevel% I get 1. So the following
script

TextTool.exe
echo Errorlevel is %errorlevel%
if errorlevel 0 (
echo Exited with 0
)

always echoes:
Errorlevel is 1
Exited with 0

If I instead perform the check:
TextTool.exe
if %errorlevel%==0 (
echo Exited with 0
)

it works fine.
HOW COME???

Thanks
Jeeji

This is because your test is flawed. The line
if errorlevel 0 . . .
means: If the errorlevel is 0 or greater than 0 then . . . - which is always
the case!

Much better to use the inbuilt %errorlevel% variable like so:
if %ErrorLevel% EQU 0 . . .

Type if /? at the Command Prompt to see the various operators available for
the "if" command.
 
Back
Top