I want to obtain the same functionaty that is offered by bash set -e
but inside Windows batch files. Is this possible how?
Currently this allow me to exit but I don't want to add this line after each line of the batch file.
IF %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL%
-e errexit
Exit immediately if a simple command exits with a non-zero
status, unless the command that fails is part of an until or
while loop, part of an if statement, part of a && or || list,
or if the command's return status is being inverted using !.
http://ss64.com/bash/set.html
You will have to add a check after each command you want to exit upon error. Instead of a whole separate line you can just use an or check ||
on the command result.
command || exit /b
This can also be simplified by putting it in a variable at the beginning.
set "e=|| exit /b"
command1 %e%
command2 %e%
The context of the your batch file is important. This should allow you to launch multiple files and exit on a true errorlevel.
@echo off
for %%a in (
"exeone"
"exetwo"
"exethree"
"exefour"
) do "%%~a" || exit /B %ERRORLEVEL%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With