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?
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
If you want to return from a CALL, you use EXIT command with /B argument (as "EXIT" alone will terminate the batch file).
For example:
CALL :SUB_ONE CALL :SUB_TWO GOTO :EOF :SUB_ONE ECHO Hello from one EXIT /B :SUB_TWO ECHO Hello from two EXIT /B :EOF
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With