user2096457
user2096457

Reputation:

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?

Upvotes: 1

Views: 416

Answers (2)

user6811411
user6811411

Reputation:

It is possible, but it was debated if it is a feature or bug.

::Batch_A.bat
@Echo off
echo You are in (%~nx0)
call :BAR
timeout -1
Goto :Eof
:BAR
echo You are in (%~nx0) (%0)
:: this runs's the batch without a call
"%~dp0Batch_B.bat" %*

:: Batch_B.bat
Goto :Eof
:FOO
echo You are in (%~nx0) and you have failed. 
Goto :Eof
:BAR
echo You are in (%~nx0) and you have succeeded.
Goto :Eof

> batch_a
You are in (Batch_A.bat)
You are in (Batch_A.bat) (:BAR)
You are in (Batch_B.bat) and you have succeeded.

Upvotes: 1

geisterfurz007
geisterfurz007

Reputation: 5874

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.

Upvotes: 5

Related Questions