Search code examples
batch-filecall

Call in batch script doesn't go to the specified subroutine in another batch file


I have two batch scripts:

Batch_A

echo You are in Batch A
call "%~dp0Batch_B.bat" BAR

Batch_B

:FOO
echo You are in Batch A and you have failed.
:BAR
echo You are in Batch A and you have succeeded.

For the life of me, no matter which way I syntax it, line 2 in the first batch does not call the "BAR" subroutine in Batch_B.

I've tried it as:

 call "%~dp0Batch_B.bat BAR"
 call "%~dp0Batch_B.bat" :BAR
 call "%~dp0Batch_B.bat" %BAR%
 call %~dp0Batch_B.bat BAR

Nothing works. I know it's probably something rudimentary, but what am I doing wrong? Are there any other methods of accomplishing this?


Solution

  • You cannot call a label in another batch-file as far as I know. What you can do is the following:

    in Batch_B.bat:

    Goto %~1
    :FOO
    echo You are in Batch A and you have failed.
    :BAR
    echo You are in Batch A and you have succeeded.
    

    And in Batch_A.bat

    call "%~dp0Batch_B.bat" BAR
    

    So this will be evaluated to Goto Bar in Batch_B.bat and will then go to the second label.

    In addition to that you should add Goto eof after the end of your :FOO part so that you do not go through the :BAR part as well.