[ACCEPTED]-Why does calling a nested batch file without prepending "call" to the line exit the parent batch file?-cmd

Accepted answer
Score: 37

DOS used simple text processing (back when 14 you had things like FILES=20 in config.sys to allow 13 20 file handles), so opened the file, read 12 the next line, closed the file, then executed 11 the line just read. If the file called another, then 10 the processing continued with that file, so 9 only 1 file handle would be required for 8 a batch file.

Until Microsoft put in the 7 call command, there was no way to get back to 6 the original file (without using tricks 5 like giving the name of the previous file 4 as a parameter, and using temporary files 3 to let the original batch file know it had 2 dome some processing, and could then GOTO the 1 next part of the file).

Score: 8

As Sean Cheshire wrote, it's necessary for 11 backward compatibility.

But starting a 10 batch file from a batch file without using 9 CALL does not terminate the parent!
It looks 8 that way, as the parent normally will not 7 executed further after the second batch 6 exits.
But using a call before starting 5 the second.bat, will show that the first 4 batch isn't terminated.


echo parent.bat
call :myLabel
echo back in parent.bat main
exit /b

second.bat & echo back in parent.bat
exit /b


echo second.bat
exit /b

I use here the 3 the secpond.bat & echo back ... to avoid another bug/feature of cmd.exe.
If 2 you use second.bat without any extras it will start 1 second.bat AND jump to the label :myLabel in second.bat!

Score: 0

Call is basically saying "go execute this 10 other batch file, and then come back here 9 and continue". It has been there since 8 DOS 3.3 or so, and if it were removed now 7 would break all backward-compatibility (which 6 is why people are still using batch scripts). It 5 can also be used to branch to :link locations.

For 4 info on the use and syntax (for future reference 3 for others), you can see this MS TechNet link

If you need 2 new functionality, use CMD scripts or PowerShell 1 scripts instead.

More Related questions