exit

EXIT

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

Syntax EXIT [/B] [exitCode] Key /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.

You should never attempt to directly write to the %errorlevel% variable, (i.e. don't try anything like SET errorlevel...) using the EXIT command provides a safe way to alter the value of the built-in errorlevel variable.

Examples

:: Exit if a required file is missing

@echo off

If not exist MyimportantFile.txt Exit /b

Echo The file was found

:: Set the error level to 5

@echo off

call :setError

echo %errorlevel%

goto :eof

:setError

exit /B 5

To make this more flexible you can change the subroutine to set any errorlevel like this:

:setError

exit /B %1

Now you can call the subroutine: call :setError 6 replacing 6 with whatever value you need the errorlevel to be set to.

Gentlemen you can't fight in here this is the war room." - President Muffley (Dr. Strangelove)

Related:

VERIFY - Provides an alternative method of raising an errorlevel without exiting

KILL - Remove a program from memory

Powershell: Exit - Exit Powershell

Equivalent bash command (Linux): break - Exit from a loop