简体   繁体   English

如何将一个变量从被调用的批处理文件返回给调用者?

[英]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.

相关问题 批处理文件:返回给调用者? - Batch File: Return to caller? 如何从批处理文件中的函数返回值? - How do I return a value from a function in a batch file? 如何“回显”批处理文件中的变量? - How do I “echo” a variable in a batch file? 如何将字符串变量从VBS文件返回到批处理文件 - How to return string variable from vbs file to batch file 如何将输出从可执行文件重定向到批处理文件的调用方? - How to redirect output from an executable to a batch file's caller? 我如何从批处理文件调用具有2个参数的Java脚本函数并将结果返回到dos变量 - how I can call from batch file a java script function with 2 arguments and return the result to a dos variable 如何在批处理文件中将输出存储在Windows“where”命令的变量中? - How do I store output in a variable from Windows “where” command, in a batch file? 如何创建批处理文件,以从2个不同的变量列表创建多层文件夹? - How do I create a batch file to create many layers of folders from 2 different variable lists? 如何将变量放入批处理文件中的 FOR /F cmd 中? - How do I put a variable into a FOR /F cmd in a batch file? 如何在Windows批处理中将环境变量的内容添加到文件中? - How do I add the content of an environment variable to a file in windows batch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM