批处理脚本中的调用不会转到另一个批处理文件中的指定子例程

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

我有两个批处理脚本:

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.

对于我来说,无论我用哪种语法,第一批中的第 2 行都不会调用 Batch_B 中的 "BAR" 子例程。

我已经试过了:

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

没有任何效果。我知道这可能是基本的东西,但我做错了什么?还有其他方法可以实现吗?

据我所知,您不能在另一个批处理文件中调用标签。您可以做的是:

在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.

并在 Batch_A.bat

call "%~dp0Batch_B.bat" BAR

所以这将在 Batch_B.bat 中被评估为 Goto Bar,然后将转到第二个标签。

除此之外,您应该在 :FOO 部分结束后添加 Goto eof,这样您就不会再经历 :BAR 部分。

这是可能的,但它是一个特性还是一个 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.