38

I understand how to call nested batch files from within a parent file using the call command, as there are plenty of resources on that:

However, I don't understand why calling another batch file from another terminates the parent.

For a less abstract example, suppose I have a batch file that "links" together separate batch files, and I erroneously didn't prepend call to each line:

foo.bat
bar.bat

This would only execute foo.bat and then exit. To correctly execute both commands, I would have to prepend call before each statement:

call foo.bat
call bar.bat

Why does the first functionality still exist? Why hasn't it been changed? I noticed that call was introduced in MS-DOS 3.3, which was released in the late 1980s, so is this functionality still here for reverse compatibility?

I can't think of any (practical) usages of it, but perhaps I'm too used to "new" programming techniques.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brandon Amos
  • 940
  • 1
  • 9
  • 19
  • Possible duplicate of *[Why does only the first line of this Windows batch file execute but all three lines execute in a command shell?](https://stackoverflow.com/questions/4036754/why-does-only-the-first-line-of-this-windows-batch-file-execute-but-all-three-li)* – Peter Mortensen Nov 01 '18 at 16:26

3 Answers3

37

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

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

SeanC
  • 15,695
  • 5
  • 45
  • 66
  • Okay, so the initial functionality was merely overlooked? `call` was implemented because people commonly used workarounds as you described? They didn't overwrite the initial functionality simply for backwards compatibility? – Brandon Amos Jul 24 '12 at 21:32
  • 5
    right. Backwards compatibility is what allows a lot of win 3.0 programs to still run on win 7. Back then, I ended up writing some functions in assembler so that it would be a program and could come back to the batch file - what is now `choice`, I implemented using a simple program that set the error level to the ASCII value of the character pressed. – SeanC Jul 24 '12 at 21:40
  • As an aside, the functionality of reading a line, executing it, reading another line, etc. means that you can have self-modifying batch files. I've never done this on purpose -- but if you have a batch file that retrieves (a new version of) itself from revision control you can get weird results where it executes version 1 up until the line that does the retrieval, then continues with version 2. – yoyo Dec 05 '14 at 16:44
8

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

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

parent.bat

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

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

second.bat

echo second.bat
exit /b

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

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    I would note that `/B` was introduced in NT 4 - previous to that, only horrible programming tricks were available – SeanC Dec 05 '14 at 17:43
0

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

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

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

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks for the answer, but that's not quite what I was asking. You also duplicated some information I gave in my question. I understand how `call` works and there are plenty of reference for people who don't. I was wondering when the other way (not using `call`) would be practically used in a batch script. Why would somebody want the parent to stop no matter what if a batch file is nested within another one? – Brandon Amos Jul 24 '12 at 20:47
  • 2
    Because maintaining a stack of where you left off would increase your resident memory segment size, which would limit the maximum size of program that could be run. Once that decision was made, it can't be changed because it would make existing batch files stop working. – Raymond Chen Jul 24 '12 at 21:29