Exiting batch with `EXIT /B X` where X>=1 acts as if command completed successfully when using && or || operators between batch calls

I'm trying to chain a series of .bat files using the EXIT /B X command to return success or failure and && and || for conditional running of the next .bat (e.g. a.bat && b.bat).

Regardless of whether I call EXIT /B 0 or anything else to end a.bat, a.bat && b.bat will call b.bat afterward. My understanding is that EXIT /B 0 should set ERRORLEVEL=0, which is success, so the && should continue. The counterpoint to this is that calling EXIT /B 1 should set ERRORLEVEL=1 which is failure, so the && should stop. What am I missing here?

Trivialized example:

For non-batch commands, acting as expected

C:\>echo test|findstr test>NUL && echo yes
yes

C:\>echo test|findstr test>NUL || echo yes

C:\>echo test|findstr nope>NUL && echo yes

C:\>echo test|findstr nope>NUL || echo yes
yes

C:\>

Using EXIT /B always sees a.bat as successful

C:\>echo @EXIT /B 0 > a.bat

C:\>a.bat && echo yes
yes

C:\>a.bat || echo yes

C:\>echo @EXIT /B 1 > a.bat

C:\>a.bat && echo yes
yes

C:\>a.bat || echo yes

C:\>

How can I exit from a.bat so that a.bat && b.bat and a.bat || b.bat behave as expected?

All commands are run in cmd.exe on Windows XP SP3.

This question and answers originated from www.stackoverflow.com
Question by (1/8/2011 7:24:34 AM)

Answer

If you ask me, exit codes in batch files are broken for this exact reason, but there is a hacky workaround you can use. As the last line of your batch file, use:

@%COMSPEC% /C exit 1 >nul

Since this is an actual process that is started you get a real process exit code and && and || will work.

Answer by

Find More Answers
Related Topics  batch  batch-file  dos  exit  errorlevel
Related Questions
  • Create event with the batch files

    Here's what i want to achieve. We have this email archive database which we optimize on a weekly basis . At the moment we are manually logging in run the command and monitor the status. The optimiza…
  • Batch Script and Exit

    I have a main batch script that calls out to some sub batch scripts, which end with the EXIT keyword. This, in turn, let me out to the command prompt again. Is there any way to capture the EXIT in t…
  • Detecting if a file is open in a batch file

    Say I have a batch file for carrying out a long build and at the end it creates an EXE. If I forget to close the app down before I start the build, the link phase fails when it can't re-create the E…
  • Windows batch-file errorlevel question

    I've got a batch file that parses a bunch of file names out of a text file and concatenates them into a single strong - it was previously discussed here . However, I don't want the string to contain…
  • Logical operators ("and", "or") in DOS batch

    How would you implement logical operators in DOS Batch files?
  • Why does makensis.exe return errorlevel 1 even though it worked?

    I have a NSIS script that works. It compiles, the produced installer works fine. And yet, makensis.exe returns 1 instead of 0. This is a real pain because I use it in a continuous integration setup …
  • Batch File - Shutdown Computer after X Minutes

    I want to make a batch file that takes a user input in minutes, and will shutdown the computer after the time is up. I know how to shutdown the computer after a set amount of time is up, I've jus…
  • Extract date using dos batch script in specific format

    I need to extract the current date and time from the system and create SQL queries using a range of dates. As i am running this on Windows, it will have to be a dos batch script. Is there a way a …
  • Batch variable not incrementing!

    SET /a _count = 1 set /p drive=Please type drive name[C/D/E/F/..]?: echo Creating Directory %drive%:\AKI_Data_Feeds mkdir %drive%:\AKI_Data_Feeds for /f %%a in (companyList.txt) do ( echo Creatin…
  • Windows batch file: call more than one command FOR each loop

    Is it possible in batch file to call more than one command in a single FOR loop, let's say for example I want to print the file name and after delete it. @ECHO OFF FOR /r %%X IN (*.txt) DO (ECHO …