"call exit" - Exit batch file from call subroutine

Microsoft Windows
Post Reply
PiotrMP006
Posts: 19
Joined: 2021-Sep-01, 10:57 am

"call exit" - Exit batch file from call subroutine

Post by PiotrMP006 »

Code: Select all

call exit
Is this the only effective way to exit batch file from call subroutine?
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: "call exit" - Exit batch file from call subroutine

Post by Simon Sheppard »

I tend to use Goto :eof

Both options are described here: https://ss64.com/nt/call.html
PiotrMP006
Posts: 19
Joined: 2021-Sep-01, 10:57 am

Re: "call exit" - Exit batch file from call subroutine

Post by PiotrMP006 »

Both options are described here: https://ss64.com/nt/call.html
The "call exit" solution is not described here "https://ss64.com/nt/call.html" !!!
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: "call exit" - Exit batch file from call subroutine

Post by Simon Sheppard »

PiotrMP006 wrote: 2022-Jul-18, 6:54 am
Both options are described here: https://ss64.com/nt/call.html
The "call exit" solution is not described here "https://ss64.com/nt/call.html" !!!
It is described in the section:
CALL a subroutine (:label)
poit
Posts: 2
Joined: 2022-Dec-09, 10:11 pm

Re: "call exit" - Exit batch file from call subroutine

Post by poit »

ok but is there a way to exit the batch from within a subroutine without closing the running shell?
like call exit /b, that's not doing it :roll:
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: "call exit" - Exit batch file from call subroutine

Post by Simon Sheppard »

Whether the host shell exits will depend a lot on how you initially call the batch file.

If you are running it from the command prompt, it will generally return to that prompt.
If you are running it by double clicking or running a shortcut to the batch, that generally starts a new shell which will end when the batch ends.

CMD /K can be used to start a batch file and then have the shell remain open afterwards.
poit
Posts: 2
Joined: 2022-Dec-09, 10:11 pm

Re: "call exit" - Exit batch file from call subroutine

Post by poit »

let me clarify:

what i meant is leaving more than one level of subroutine nesting with a single effort.
like plain exit does (but is just rude and should never be used :P )

like maybe for a generic "print-Error & ret-status", potentially saving you lots of exit /b glue
seeing how there a numerous ways to mess with return positions, it sure "feels" possible.
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: "call exit" - Exit batch file from call subroutine

Post by jeb »

Yes it's possible to leave a function or even nested functions.

I know three different ways.

1. Syntax Error
A syntax error stops immediately a batch file.

Code: Select all

@echo off

call :func1
echo End of main
exit /b

:func1
echo Start of :func1
call :func2
echo End of :func1
exit /b

:func2
echo Start of :func2
call :syntaxError 2> nul
echo End of :func2
exit /b

:SyntaxError
()
2. Simulate CTRL-C
The key is the CTRL-C exitcode

Code: Select all

cmd /c exit -1073741510
See the full answer from dbenham at https://stackoverflow.com/a/25474648/463115
It works, because a batch file stops when it detects this special exit code.

3. Exception handling
The key command here is

Code: Select all

(goto) 2> NUL
For an extensive explanation see https://stackoverflow.com/a/31445372/463115

Summary
The syntax error has several drawbacks:
- It stops all batch instances not only the currently running script, also the caller script (if it was called by a batch file)
- The setlocal scopes are not reverted, sometimes this can be useful for debugging, but most of the time this is unwanted behavior
- You can not set the errorlevel

CTRL-C is better, but:
- It stops all batch instances not only the currently running script, also the caller script (if it was called by a batch file)
- You can not set the errorlevel

Exception handling is the best, but a bit more complex
- You can control the errorlevel
- You can stop at any level of the nesting
Post Reply