exit /b in batch files

Feb 27, 2018
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 )
 
May 20, 2008
11,406
99
Syracuse, NY, USA
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
 

rconn

Administrator
Staff member
May 14, 2008
12,348
150
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.
 

rconn

Administrator
Staff member
May 14, 2008
12,348
150
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.
 
Feb 27, 2018
11
0
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
Thread starter Title Forum Replies Date
vefatica Exit code of a batch file? Support 4
S How to? Runs start /w in invisible mode OR run program after exit of another one Support 3
vefatica ON EXIT? Support 8
rps Reboot and exit reason Support 10
H lua Causes Take Command Tab to Exit Support 5
Stefano Piccardi MKLNK exit status Support 9
ed neff How to? exit TCC/Take Command to a new directory Support 10
M Why is the exit code zero? Support 6
SeoulBigChris Save Environment (Tabs) on Exit? Support 3
Stefano Piccardi EXIT hangs Support 5
R TC 13 / Using TCDIALOG to exit commands Support 4
M Not real important but kind of annoying EXIT issue... Support 20
lassevk exit(1) in a python script exits console, by design or bug? Support 1
cgunhouse IDE Crashes on Exit Support 6
J tcmd.exe/tcc.exe appcrash on exit Support 4
fpefpe How to? shortcuts and batch files Support 4
vefatica `Back quotes` - command line vs. batch file Support 5
fpefpe How to? batch file size Support 2
FreezerBurnt Help making a CMD and TCC compatible batch file Support 7
M Oracle batch OPatch.bat.bat not working in TCC but correctly in CMD Support 5
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
P Kill all other instances of tcc from a btm batch file Support 2
fpefpe batch file ending early Support 18
R Batch Debugger Command Expansion Window Support 3
K robocopy cancelling batch file processing Support 28
A Unable to pass batch file arguments unaltered Support 18
K Batch debugger not stepping correctly Support 4
S incorrect message at start Cancel batch job 4START.bat ? (Y/N/A) : Support 1
MickeyF Why does this batch file give me 'unknown command "else"' error? Support 17
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
P Labels in batch files: TCC vs CMD.exe Support 9
vefatica Batch parameters broken? Support 2
cxxl Set encoding for batch file Support 4
dcantor How to? Batch file to be executed by TCSTART Support 4
J New lexer for TCC batch files. Support 0
T Fixed Problem with use of Batch parameters in the IDE Support 1
T Fixed IDE locks up if edit window for running batch is closed Support 6
T How to? Show the values of batch variables on the TCC screen Support 21
D Alias in batch file Support 3
TT's Help! Uninstalling TCC did not restore cmd.exe as Win10 default batch file processor. Support 4
J cd command altered in batch file Support 3
MickeyF running a program from cmdline versus in batch file Support 1
B Batch file won't run if there is already an instance of TC 17 running Support 9
nickles Fixed Executing long command lines in a batch generates file name errors Support 16
T batch execution Support 9
rjperrella start /tabna leaves blank windows when running batch scripts Support 10
jadaml Echo unicode characters from UTF-8 Batch files? Support 1
J Files Disappearing During Batch Execution Support 4
nickles Fixed In batch files, %0 now evaluates to the absolute path Support 9

Similar threads