I have the following problem:
I have created a batch script which calls itself in there (for being able to write a log in parallel). In the script I start another pr
There's a good explanation of all the options for exiting a batch script here: http://www.robvanderwoude.com/exit.php
Specifically, from that page:
The DOS online help (HELP EXIT) doesn't make it clear that the /B parameter exits the current instance of script which is not necessarily the same as exiting the current script. I.e. if the script is in a CALLed piece of code, the EXIT /B exits the CALL, not the script.
So you definitely don't want exit /b 0
in this case. If just exit 0
doesn't work, try GOTO:EOF
.
I guess your problem lies within the start
command. The following excerpt from the start /? help might point to the issue:
command/program
If it is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.
If it is not an internal cmd command or batch file then it is a program and will run as either a windowed application or a console application.
As a solution you could try to modify the start command like this:
start "" cmd /c "startServer.bat"
The earlier answer from Vicky is very good. There is some additional undocumented (or, at least, unclear) behaviour going on here.
In your question, you have a somewhat more complicated situation, but let's say you are calling/starting a batch file from the original, using exit /b 0
in the called batch file, and expecting that the ERRORLEVEL is accessible in the original.
@echo off
start "" /b /wait cmd /c "startServer.bat"
if ERRORLEVEL 1 echo Exit code is one & exit /b 1
if ERRORLEVEL 0 echo Exit code is zero & exit /b 0
@echo off
exit /b 0
To get this to work, the start
command must be used with the certain options. Depending on the options, they may need to be in a specific order. (!)
According to the docs at SS64 on Start, you should be able to use the /b
and /wait
switches. The documentation does not state that the order of these switches matters, but it does.
For instance, this will NOT work (commands run out of order, and ERRORLEVEL is not returned):
start "" /wait /b cmd /c "startServer.bat"
But this does work exactly as expected:
start "" /b /wait cmd /c "startServer.bat"
The only difference is swapping the /b
and /wait
switches.
I discovered this by accident, using the following steps:
start
and call
and cmd
I did not try anything new, I just started over, and it worked the first time. Comparing to previous file versions showed me this (apparently) small difference. Turns out, there is no such thing as a "small" change!