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

问题描述 投票:1回答:2

我有两个批处理脚本:

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-file call
2个回答
6
投票

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

在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,然后转到第二个标签。

除此之外,你应该在你的Goto eof部分结束后添加:FOO,这样你也不会通过:BAR部分。


1
投票

这是可能的,但如果它是一个功能或错误,它是有争议的。

::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.
© www.soinside.com 2019 - 2024. All rights reserved.