Quit the current batch script, quit the current subroutine or quit the command processor (CMD.EXE) optionally setting an errorlevel code.

      EXIT [/B] [exitCode]

    /B        When used in a batch script, this option will exit 
              only the script (or subroutine) but not CMD.EXE

   exitCode   Sets the %ERRORLEVEL% to a numeric number.
              If quitting CMD.EXE, set the process exit code no.

To close an interactive command prompt, the keyboard shortcut ALT + F4 is an alternative to typing EXIT.


EXIT /b has the option to set a specific errorlevel, 0 for sucess, 1 or greater for an error.
EXIT without an ExitCode acts the same as goto:eof and will not alter the %ERRORLEVEL%

You should never attempt to directly write to the %ERRORLEVEL% variable, (SET errorlevel...) instead use the EXIT command as a safe way to alter the value of the internal ERRORLEVEL pseudo variable.

When EXIT /b used with FOR /L, the execution of the commands in the loop is stopped, but the loop itself continues until the end count is reached. This will cause slow performance if the loop is (pointlessly) counting up to a large number.
In the case of an infinite loop, this EXIT /b behaviour will cause the script to hang until manually terminated with Ctrl + C

Exiting nested FOR loops, if EXIT /b can be used to exit a FOR loop that is nested within another FOR loop.
This will only work if the inner FOR loop is contained in a separate subroutine, EXIT /b terminates the subroutine.


Exit if a required file is missing
@Echo Off
If not exist MyimportantFile.txt Exit /b
Echo If we get this far the file was found

Set the errorlevel to 5
@Echo Off
call :setError
echo %errorlevel%
goto :eof

Exit /B 5

Using EXIT /b to exit a nested FOR loop (so skipping the values X,Y andZ), but still continuing the main loop:

@Echo Off
For %%A in (alpha beta gamma) DO (
   Echo Outer loop %%A
   call :inner 
goto :eof

For %%B in (U V W X Y Z) DO (
   if %%B==X ( exit /b 2 )
   echo    Inner loop    Outer=%%A Inner=%%B

EXIT is an internal command.

“Making music is not about a place you go. It’s about a place you get out of. I’m underwater most of the time, and music is like a tube to the surface that I can breathe through. It’s my air hole up to the world. If I didn’t have the music I’d be under water, dead” ~ Fiona Apple


VERIFY - Provides an alternative method of raising an errorlevel without exiting
TSKILL - End a running process
Powershell: Exit - Exit Powershell or break - Exit a program loop
Equivalent bash command (Linux): break - Exit from a loop

© Copyright SS64.com 1999-2017
Some rights reserved