Page 1 of 1

"call exit" - Exit batch file from call subroutine

Posted: 17 Jul 2022 04:56
by PiotrMP006

Code: Select all

call exit
Is this the only effective way to exit batch file from call subroutine?

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

Posted: 17 Jul 2022 06:00
by aGerman
Reading Dave's reply there (and following the links) may give you an idea.
viewtopic.php?t=6996#p45553

However, in most cases I think something like that might be sufficient:

Code: Select all

@echo off &setlocal
call :sub
echo this is not printed anymore, however the exit /b 42 from :sub is executed in the context of the main code
goto :eof

:sub
2>nul (goto) &exit /b 42
echo this won't be seen after (goto)
goto :eof
calling it:

Code: Select all

Microsoft Windows [Version 10.0.22000.795]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\steffen\Desktop>test.bat

C:\Users\steffen\Desktop>echo %errorlevel%
42

C:\Users\steffen\Desktop>
Steffen

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

Posted: 17 Jul 2022 06:24
by PiotrMP006
Why is the "call exit" command bad?

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

Posted: 17 Jul 2022 06:36
by aGerman
I didn't say that it is bad per se. However:
1) You asked for an alternative.
2) "Exit" will always terminate the cmd.exe process.
3) I don't even understand what the "call" in you "call exit" is good for. A bare "exit" is going to do the same thing.

Steffen