windows - 我如何调用anotherfile.bat :label correctly in Batch?

标签 windows batch-file cmd call method-call

<分区>

我有一个批处理文件,它是一个函数库

:findmsbuild
if exist msbuildpath.txt (
    for /f %%i in (msbuildpath.txt) do set MSBUILD="%%i\MSBuild.exe"
) else (
    set VSWHERE="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
    for /f "delims=" %%i in ('!VSWHERE! -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe') do set MSBUILD="%%i"
)
exit /b

还有 official documentation for call 说我应该能够直接调用另一个文件中的标签

call library.bat :findmsbuild

但实际情况是,解释器从第一行开始执行 library.bat。我究竟做错了什么?调用批处理文件大约是

setlocal enableextensions enabledelayedexpansion
cd "%~dp0"
call library.bat :findmsbuild
echo %MSBUILD%

最佳答案

你做错了什么?信任 Microsoft 的文档。

您可以调用 externalbatch 或调用 :internalroutine。

为了做你想做的事,你需要在你的库文件的开头放一行像 goto %1 - 然后记住参数列表的开头有标签,所以你需要转移它,%* 将保留标签作为第一个参数。

关于windows - 我如何调用anotherfile.bat :label correctly in Batch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72687310/

相关文章:

windows - 使用Rundll32复制SUBST的功能

linux - 连接到托管在 CentOS 上的 MongoDb 服务器失败

c++ - 从 Linux 到 Windows 交叉编译时应该如何处理字符编码?

bash - Bash 是否有 Batch 的 msg * 等价物?

Python 子进程无法捕获 Windows 程序的输出

c++ - 编译SkyFireEMU报错,sizeof(void *) 都不是

batch-file - 如何将参数传递给批处理文件?

windows - 批处理文件: parsing command line inputs

batch-file - 递归移动文件到根文件夹

bash - 如何从 Cygwin 调用 bat/cmd 文件并继承变量?