Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get the ERRORLEVEL in the calling batch from a sub batch

4,842 views
Skip to first unread message

Arno

unread,
Mar 2, 2011, 4:43:58 AM3/2/11
to
Hi all,

I have some difficulties to give an exit code of a called batch to the
calling one, here are the depending code:

1. Callee batch:

:
call SubBatch %SomeParameter%
echo ERRORLEVEL after SubBatch: '%ERRORLEVEL%'
if "%ERRORLEVEL%" NEQ "0" goto :EOF
:

2. SubBatch
:
call python.exe -t %SomeParameter%
if "%ERRORLEVEL%" NEQ "0" (
@echo Python exit with exitcode '%ERRORLEVEL%'
rem Give a special exit code back to the callee
rem Tried the following:
rem call exit /b 99
rem call exit 99
rem exit /b 99
exit 99
)
:
Here is the output:
Python exit with exitcode '1'
ERRORLEVEL after SubBatch: '0'

In all cases what I have tried the ERRORLEVEL in the calling batch was
0! What I am doing wrong or what can I do to achieve my target?

Many thanks for all hints
best regards

Arno

Tom Lavedas

unread,
Mar 2, 2011, 8:27:19 AM3/2/11
to

The exit /b nn works just fine for me. I even tested it as a
subroutine, as opposed to a separate batch, and it worked. My test
code was ...

call :errorcode
echo %errorlevel%
goto :EOF

:errorcode
call (echo.|find "x" > nul)
if "%errorlevel%" neq "0" (
exit /b %errorlevel%
)

I used the FIND to simulate your Python example.
_____________________
Tom Lavedas

billious

unread,
Mar 2, 2011, 9:33:27 AM3/2/11
to

I confirmed Tom's code with a similar test. I also tried combinations of
SETLOCAL with ENABLEDELAYEDEXPANSION and ENABLEEXTENSIONS and was unable to
reproduce the results reported by OP.

ISTM you have published simplified code, and the simplification has
eliminated the observed problem. The code works as published (after the EXIT
99 has been mofified to EXIT /B 99)

I suspect that you are assigning a value to ERRORLEVEL with a SET statement.
ERRORLEVEL and other pseudovariables like CD, TIME and DATE act like normal
environment variables, but return their "magic" values from the OS if they
are NOT explicitly set in the environment.

Even so, if ERRORLEVEL had been explicitly set in the environment, the
PYTHON exit report-line should have reported THAT value assigned to
ERRORLEVEL, not the errorlevel we usually understand and expect.

So - it's a puzzle. I believe we need somewhat less-censored sample.


Timo Salmi

unread,
Mar 2, 2011, 11:39:50 AM3/2/11
to
On 02.03.2011 11:43 Arno wrote:
> I have some difficulties to give an exit code of a called batch to the
> calling one, here are the depending code:

> call SubBatch %SomeParameter%


> echo ERRORLEVEL after SubBatch: '%ERRORLEVEL%'
> if "%ERRORLEVEL%" NEQ "0" goto :EOF

A point in the basics. It is prudent never to test an errorlevel further
down than on the next line. Simply because if you do, the errorlevel
value may well have changed.

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Tom Lavedas

unread,
Mar 2, 2011, 11:52:03 AM3/2/11
to
On Mar 2, 11:39 am, Timo Salmi <t...@uwasa.fi> wrote:
> On 02.03.2011 11:43 Arno wrote:
>
> > I have some difficulties to give an exit code of a called batch to the
> > calling one, here are the depending code:
> >   call SubBatch %SomeParameter%
> >   echo ERRORLEVEL after SubBatch: '%ERRORLEVEL%'
> >   if "%ERRORLEVEL%" NEQ "0" goto :EOF
>
> A point in the basics. It is prudent never to test an errorlevel further
> down than on the next line. Simply because if you do, the errorlevel
> value may well have changed.
>
>     All the best, Timo
>
> --
> Prof. Timo Salmi    mailto:t...@uwasa.fi   ftp &http://garbo.uwasa.fi/

> Hpage:http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
> Department of Accounting and Finance,   University of Vaasa,  Finland
> Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.php

In general, what you say about testing is good advice, but none of the
statements shown in the OPs sample code (Echo, If and Rem) cause a
problem in my testing. Therefore, it is probably more as billious
said, that the problem is related to code the OP did not choose to
show.
_____________________
Tom Lavedas

Todd Vargo

unread,
Mar 2, 2011, 5:33:25 PM3/2/11
to

I get the same result when using... call exit /b 99
Exitcode is dropped when the CALL statement is used with EXIT.
Both exits without /b close cmd.exe session immediately (as expected).

The correct line to use is... exit /b 99
ERRORLEVEL after SubBatch: '99'

During your testing, are you sure you saved after each edit of SubBatch?

--
Todd Vargo

(Post questions to group only. Remove "z" to email personal messages)

Arno

unread,
Mar 4, 2011, 5:49:58 AM3/4/11
to
> A point in the basics. It is prudent never to test an errorlevel further
> down than on the next line. Simply because if you do, the errorlevel
> value may well have changed.

Thanks for the hint, but I insert the echo's after I have tried it
without them
first and I think that echo's don't change the ERRORLEVEL!?
Even I have recognized, that I can not set this ERRORLEVEL by my own
after that it won't never be changed again from other calls.

I tried the example from Tom and it also works by me, also if I do it
in two
separat scripts. The point is, that the original scripts are very
large and
complex and have a lot of dependencies, so it make no sense to post
them. Perhaps it is possible that it is dependind on setlocal -
endlocal
things in my internal calling hierarchie?

regards
Arno

Arno

unread,
Mar 4, 2011, 5:54:23 AM3/4/11
to
> I get the same result when using... call exit /b 99
> Exitcode is dropped when the CALL statement is used with EXIT.
> Both exits without /b close cmd.exe session immediately (as expected).
>
> The correct line to use is... exit /b 99
> ERRORLEVEL after SubBatch: '99'
>
> During your testing, are you sure you saved after each edit of SubBatch?

Hi Todd,

I am realy sure, that I have saved the sub batch during my tests, but
I will
try it again, when I have access next time to the neccassary
environment.
That will be on monday.

Thanks
Arno

Frank P. Westlake

unread,
Mar 4, 2011, 10:06:09 AM3/4/11
to
"Arno"
news:b3c32a2a-2199-4967...@a11g2000pri.googlegroups.com...

> 1. Callee batch:
>
> :
> call SubBatch %SomeParameter%
> echo ERRORLEVEL after SubBatch: '%ERRORLEVEL%'
> if "%ERRORLEVEL%" NEQ "0" goto :EOF


Do you have a script named SubBatch in your path?

Frank


gal...@gmail.com

unread,
Feb 12, 2014, 9:46:54 AM2/12/14
to
I have just had such a problem, but as I cannot see your entire script, I am not sure we have the same problem. My cause for problem was that I was calling the subrutine inside a FOR-loop. This makes it impossible to check the ERRORLEVEL using 'if ERRORLEVEL 0' or 'if %ERRORLEVEL% EQU 0'. Do this instead:

At top of script:

Setlocal EnableDelayedExpansion

This is to be able to read variables inside the FOR-loop.
Then, when checking the errorlevel, do this:

call :SubBatch !SomeParameter!
if !ERRORLEVEL! EQU 0 ( echo errorlevel is 0 )

I know this is an old post, but it was my first hit on google and therefore I post my solution here.
0 new messages