[英]How do I return a variable from a called batch file to the caller?
我希望能够像调用 Java 中的函数一样调用批处理文件或类似的东西。 是否有可能做一些诸如
设置答案=调用Calculate.bat %input1% %input2%
其中calculate.bat 将计算两个输入的总和,然后将其分配给原始批处理文件中的answer 变量。 这在某种程度上可能吗?
如果你像@SomethingDark 所说的那样在你的脚本中使用setlocal
,你可以决定只传输某些变量
@ECHO OFF
setlocal
REM your code here, for example:
set /A "output=%1 + %2" > NUL
endlocal & set "answer=%output%"
REM variable %output% does not exist anymore
REM only %answer% remain changed (and stay the same once the script end)
就像@Harry johnston 所说的那样,如果您在批处理文件中设置了一个变量,您调用该变量将在调用者中设置为。 如果您不想在您调用的批处理文件中设置任何变量,请执行以下操作:
for /f %%a in ('calculate.bat %input1% %input2%') do set "output=%%a"
命令的输出存储在%output%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.