tags:

views:

16

answers:

2

Given the following code:

@Echo off
ECHO Start
ECHO Calling SUB_A
CALL :SUB_A
ECHO Calling SUB_B
CALL :SUB_B

:SUB_A
    ECHO In SUB_A
    GOTO:EOF

:SUB_B
    ECHO In SUB_B
    GOTO:EOF

ECHO End

I expect this output:

Start
Calling SUB_A
In SUB_A
Calling SUB_B
In SUB_B
End

But I get this:

Start
Calling SUB_A
In SUB_A
Calling SUB_B
In SUB_B
In SUB_A

What am I doing wrong here?

A: 

After your line CALL :SUB_B the batch file falls through to SUB_A. If you don't want it to, you need to put a GOTO line there.

BoltBait
+2  A: 

The line CALL :SUB_B returns, the script proceeds to the next few lines:

:SUB_A           # no effect from this one
ECHO In SUB_A    # prints message

You need to insert a GOTO:EOF after the call if you want it to stop there.

Batch files are not structured programs; they are a sequence of instructions with some BASIC-like facility for GOTO and CALL.

Edmund