exit /b does not work properly in subroutines

M

Martin Rakhmanov

Hello

According to MSFT documentation (help exit) I expect "exit /b 1" called
within a batch to exit current batch and set ERRORLEVEL to 1. But this does
not work when I call exit /b in a subroutine - see attached batch. Why? I
am running Windows Server 2003 SP1.

Martin
 
P

Phil Robyn

Martin said:
Hello

According to MSFT documentation (help exit) I expect "exit /b 1" called
within a batch to exit current batch and set ERRORLEVEL to 1. But this does
not work when I call exit /b in a subroutine - see attached batch. Why? I
am running Windows Server 2003 SP1.

Martin

How about this version?

- - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - -
=====begin c:\cmd\demo\sample.cmd ====================
01. @echo off
02. setlocal
03. set exit=goto :EOF
04. call :MyLabel %1
05. %exit%
06. echo This line should never be reached
07.
08. goto :EOF
09.
10. :MyLabel
11.
12. if NOT "%~1"=="unknownoption" (
13. echo Hello from subprogram
14. set exit=exit /b 1
15. goto :EOF
16. )
=====end c:\cmd\demo\sample.cmd ====================
- - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - -
 
M

Martin Rakhmanov

Phil

Thanks for input but this is not what I need: I should be able to quit
entire batch from within subprogram conditionally.
Also I do not understand why simple documented by Microsoft function ("exit
/b") does not work as expected.

Martin
 
D

David Candy

It is doing what is expected. It is exiting the second bat file started with call. You are running a batchfile twice.

add this line

echo %errorlevel%
echo This line should never be reached

See Call's help.
--
--------------------------------------------------------------------------------------------------
How to lose a war in Iraq
http://webdiary.com.au/cms/?q=node/1335#comment-48641
=================================================
Martin Rakhmanov said:
Phil

Thanks for input but this is not what I need: I should be able to quit
entire batch from within subprogram conditionally.
Also I do not understand why simple documented by Microsoft function ("exit
/b") does not work as expected.

Martin
 
M

Martin Rakhmanov

Yes, you are right. How do I then exit entire batch from subprogram and set
errorlevel?


"David Candy" <.> wrote in message
It is doing what is expected. It is exiting the second bat file started with
call. You are running a batchfile twice.

add this line

echo %errorlevel%
echo This line should never be reached

See Call's help.
--
--------------------------------------------------------------------------------------------------
How to lose a war in Iraq
http://webdiary.com.au/cms/?q=node/1335#comment-48641
=================================================
Martin Rakhmanov said:
Phil

Thanks for input but this is not what I need: I should be able to quit
entire batch from within subprogram conditionally.
Also I do not understand why simple documented by Microsoft function
("exit
/b") does not work as expected.

Martin
 
M

Martin Rakhmanov

Also why exit without /b switch exits entire batch, i.e. does exactly what I
need (but does not set errorlevel)?

Martin Rakhmanov said:
Yes, you are right. How do I then exit entire batch from subprogram and
set errorlevel?


"David Candy" <.> wrote in message
It is doing what is expected. It is exiting the second bat file started
with call. You are running a batchfile twice.

add this line

echo %errorlevel%
echo This line should never be reached

See Call's help.
 
D

David Candy

That's how programs tend to work. Return your error to main program, test it there and decide what to do.

--
--------------------------------------------------------------------------------------------------
How to lose a war in Iraq
http://webdiary.com.au/cms/?q=node/1335#comment-48641
=================================================
Martin Rakhmanov said:
Yes, you are right. How do I then exit entire batch from subprogram and set
errorlevel?


"David Candy" <.> wrote in message
It is doing what is expected. It is exiting the second bat file started with
call. You are running a batchfile twice.

add this line

echo %errorlevel%
echo This line should never be reached

See Call's help.
 
D

David Candy

It doesn't. It either exits a shelled batch or cmd.exe, just like all dos batch files expect..
 
M

Martin Rakhmanov

Please have a look at two batch files: they only differ in one line (exit
and exit /b 1 respectively). So why the batches behave differently?


"David Candy" <.> wrote in message
It doesn't. It either exits a shelled batch or cmd.exe, just like all dos
batch files expect..
 
M

Martin Rakhmanov

I see - /b switch is required to exit batch. Otherwise cmd.exe session is
closed. I have no more questions, thanks for help, David - it is excellent!

Martin
 
M

Martin Rakhmanov

Thank you, you are so polite.

"David Candy" <.> wrote in message
Why do you keep repeating yourself. Are you a total idiot. I just told you.
 
A

Al Dunbar [MS-MVP]

Martin Rakhmanov said:
Phil

Thanks for input but this is not what I need: I should be able to quit
entire batch from within subprogram conditionally.
Also I do not understand why simple documented by Microsoft function ("exit
/b") does not work as expected.

I realize that you have resolved your issue here, but it is worth
remembering the following truism: "when confronted with things that do not
work as expected, consider that it may be the expectation that is in error".

/Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top