Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

exit /b in batch files

Feb
11
0
The following batch file is executed differently by TCC and CMD:

if exist Child.bat (
Child.bat
exit /b
)


TCC does not launch the Child.bat and CMD does. The workaround is using "call Child.bat" instead of just "Child.bat" but TCC obviously is not fully compatible with CMD. It is funny that /b option of the "exit" command is described in the TCC documentation as "This switch is for compatibility with CMD".

It is interesting that if the first and last lines are commented out, the TCC does launch the Child.bat:

rem if exist Child.bat (
Child.bat
exit /b
rem )
 
TCC should execute child.bat (and should not return to the parent batch file, using CALL should make it return to the parent batch file). But you are right. TCC version 18 does execute child.bat. Versions 19 - 22 do not.
Code:
v:\> ver

TCC  22.00.38   Windows 7 [Version 6.1.7601]

v:\> type parent.bat
@echo off
if exist Child.bat (
echo executing child.bat ...
Child.bat
echo This is parent.bat
exit /b
)

v:\> type child.bat
@echo off
echo This is child.bat

v:\> parent.bat
executing child.bat ...
This is parent.bat

Code:
v:\> ver

TCC  18.00.32   Windows 7 [Version 6.1.7601]

v:\> type parent.bat
@echo off
if exist Child.bat (
echo executing child.bat ...
Child.bat
echo This is parent.bat
exit /b
)

v:\> type child.bat
@echo off
echo This is child.bat

v:\> parent.bat
executing child.bat ...
This is child.bat
 
Neither TCC nor CMD is doing what you think they're doing.

Both of them terminate the current batch file when they see the "child.bat" command, and then switch to executing "child.bat". When CMD finishes child.bat, it tries to run your EXIT /B, but there isn't any batch file still running, so the command is ignored.

In TCC's case, it is mistakenly trying to run EXIT /B before child.bat, so nothing happens. I will fix that in the next build.

But the correct syntax, which will work in both TCC and CMD, is to remove the "EXIT /B" command. It serves no purpose and is just taking up space.
 
TCC should execute child.bat (and should not return to the parent batch file, using CALL should make it return to the parent batch file). But you are right. TCC version 18 does execute child.bat. Versions 19 - 22 do not.

Your test is flawed - TCC should be running the remainder of the commands in the command group after it chains to child.bat (because that's what CMD does, dumb or not). In v18 it does not, so v18 is incompatible. V22 fails only if you put in the useless EXIT /B.
 
You're right ... and it sure is dumb. IFF makes life a lot better.
 
Originally it was "call child.bat", that's why it needed "exit /b". I removed "call" making "exit /b" unnecessary to test how it works. If an extremely unnecessary "exit /b" is removed, it works ok :)

if exist Child.bat (
Child.bat
::exit /b
)
 

Similar threads

Back
Top