【问题标题】:Call in batch script doesn't go to the specified subroutine in another batch file批处理脚本中的调用不会转到另一个批处理文件中的指定子程序
【发布时间】:2026-01-18 11:20:02
【问题描述】:

我有两个批处理脚本:

批次_A

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

批次_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


    【解决方案1】:

    据我所知,您不能在另一个批处理文件中调用标签。您可以执行以下操作:

    在 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 部分。

    【讨论】:

      【解决方案2】:

      这是可能的,但它是一个功能还是一个错误存在争议。

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

      【讨论】:

      • batch_a.bat 中被调用的子程序会给%0%~nx0 提供不同的结果。后者将返回真实文件,而%0 命名被调用的子。
      最近更新 更多