lastr2d2
lastr2d2

Reputation: 3968

how to exit from parent batch context

I 'm trying to create a batch file as below.

@ECHO OFF
:Main
CALL :STEP_1
CALL :STEP_2

GOTO :EXIT

:STEP_1
REM some other logic here
IF %ERRORLEVEL% NEQ 0 (
  GOTO :ERROR
)
GOTO  :EOF

:STEP_2
REM step 2 logic
GOTO  :EOF

:ERROR
EXIT /b 1

:EXIT
EXIT /b 0

What I'm expecting is that the error handling in :STEP_1 will exit from the whole batch file. However, what is happening is that it only exit from the content we created when we use CALL. :STEP_2 will still be called even if there is an error in :STEP_1.

My question is, is it possible to accomplish my requirement? To exit from the whole batch file instead of the :STEP_1 context, so :STEP_2 won't be called.

Upvotes: 1

Views: 81

Answers (1)

Mayura Vivekananda
Mayura Vivekananda

Reputation: 674

You can use the following (GOTO) 2>NUL combined with an exit, and that should do the job for you.

:ERROR
(goto) 2>nul & exit /b 1

Refer to this for more information.

Upvotes: 4

Related Questions