"call exit" - Exit batch file from call subroutine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PiotrMP006
Posts: 31
Joined: 08 Sep 2017 06:10

"call exit" - Exit batch file from call subroutine

#1 Post by PiotrMP006 » 17 Jul 2022 04:56

Code: Select all

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 17 Jul 2022 06:00

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

PiotrMP006
Posts: 31
Joined: 08 Sep 2017 06:10

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

#3 Post by PiotrMP006 » 17 Jul 2022 06:24

Why is the "call exit" command bad?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 17 Jul 2022 06:36

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

Post Reply