11

When using the CALL command to call a label in a batch script, and you end the sub-routine with GOTO:eof, what happens from there? Does it return back to where the sub-routine's CALL is located? Or does it continue on after the location of the call script?

For example:

ECHO It's for my college fund.
CALL :OMGSUB
ECHO *runs away and cries like a little girl*

:OMGSUB
ECHO Your mom goes to college.
GOTO:eof

ECHO *picks up jewelry box*

After GOTO:eof which line will it echo next?

Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
  • See http://www.robvanderwoude.com/goto.php for an example of how to use labels to create "subroutines" and how to exit the batch without repeating those content within the subroutine. – JohnLBevan Jul 04 '16 at 09:25
  • You could also replace `GOTO:eof` with `exit /b`. Also, `ECHO Your mom goes to college.` will be executed after `ECHO *runs away and cries like a little girl*`, which may not be intended. The solution would be to add an `exit` after `ECHO *runs away and cries like a little girl*`. – FluorescentGreen5 Jul 20 '19 at 10:22
  • See also [Where does GOTO :EOF return to?](https://stackoverflow.com/a/37518000/3074564) – Mofi Jul 20 '19 at 11:52

2 Answers2

13

Why not just run it and see for yourself? I saved your script to a batch file called foo.bat, and I changed the Your mom goes to college. to have an echo in front.

C:\temp>foo

C:\temp>ECHO It's for my college fund.
It's for my college fund.

C:\temp>CALL :OMGSUB

C:\temp>echo Your mom goes to college.
Your mom goes to college.

C:\temp>GOTO:eof

C:\temp>ECHO *runs away and cries like a little girl*
*runs away and cries like a little girl*

C:\temp>echo Your mom goes to college.
Your mom goes to college.

C:\temp>GOTO:eof

C:\temp>

So it's easy to see that after the OMGSUB is called, it

  1. Goes to the end of file.
  2. Then it returns to the line right afer the CALL :OMGSUB and echos the "runs away" line
  3. Then it echos the Your Mom line again
  4. Then it goes to end of file and terminates
  5. The line echo *picks up jewewlry box* never gets reached.
FluorescentGreen5
  • 879
  • 11
  • 23
dcp
  • 54,410
  • 22
  • 144
  • 164
  • 5
    because why not ask it in a question, have it get answered, you can get points, people who look it up can find out right away and save some time, and everyone=happy? XD yes I could have easily put it in a batch script but why not just have it documented? And thank you for the answer. I wasn't sure about that last part, it re-iterating over the subroutine even after it is called. – Anthony Miller Jul 18 '11 at 19:21
  • 2
    Mechaflash, this is trivial to try, actually. I doubt this is a good question that plenty of others will stumble over and be happy to find an answer. But I might be proven wrong. – Joey Jul 18 '11 at 19:41
  • 2
    You must note that any command placed after a goto will never be executed, like your ECHO *picks up jewelry box*. To avoid these confussions I suggest to never use goto :eof and use exit /b instead. – Aacini Jul 22 '11 at 16:32
  • 2
    C:\>HELP CALL should give you more info. – PollusB Jan 29 '14 at 20:33
0

GOTO:eof ends the OMGSUB subroutine in your batch file. So ECHO *runs away and cries like a little girl* executes next, but ECHO Your mom goes to college. will also execute again.

Sidenote which was also a comment:

You could replace GOTO:eof with exit /b. Also, ECHO Your mom goes to college. will be executed after ECHO *runs away and cries like a little girl*, which may not be intended. The solution would be to add an exit after ECHO *runs away and cries like a little girl*.

FluorescentGreen5
  • 879
  • 11
  • 23