[英]How do I return a variable from a called batch file to the caller?
I want to be able to call a batch file like it were a function in Java or something of the like.我希望能够像调用 Java 中的函数一样调用批处理文件或类似的东西。 Would it be possible to do something such as
是否有可能做一些诸如
set answer=call Calculate.bat %input1% %input2%设置答案=调用Calculate.bat %input1% %input2%
Where calculate.bat would calculate the sum of the two inputs, then it would assign it to the answer variable in the original batch file.其中calculate.bat 将计算两个输入的总和,然后将其分配给原始批处理文件中的answer 变量。 Is this possible in some way?
这在某种程度上可能吗?
If you use setlocal
in your script like @SomethingDark said, you could to decide to transmit only certain variable如果你像@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)
like @Harry johnston said, if you set a variable in the batch file you called the variable will set in the caller to.就像@Harry johnston 所说的那样,如果您在批处理文件中设置了一个变量,您调用该变量将在调用者中设置为。 if you do not want set any variables in the batch file you call, do:
如果您不想在您调用的批处理文件中设置任何变量,请执行以下操作:
for /f %%a in ('calculate.bat %input1% %input2%') do set "output=%%a"
the output of your command is stored in %output%
命令的输出存储在
%output%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.