如何将参数传递给另一个正在运行的批处理程序 [英] How do I pass parameters to a running batch program by another one

查看:141
本文介绍了如何将参数传递给另一个正在运行的批处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

foo.bat :
@echo off
start bar.bat 2 3
set result=//somehow return the value
echo %result%
pause
exit

bar.bat :
@echo off
set int1=%1
set int2=%2
set /a result=%int1%+%int2%
//somehow send the result to the running foo.bat
exit

有人可以告诉我如何做到这一点.我只能考虑编写一个文件并检查文件是否存在以及它包含的内容.但这在我看来很复杂.提前致谢.

can someone give me an idea on how to do this. I can only think about writing a file and checking in a for loop if the file exists and what it contains. But this is way complicated in my opinion. Thanks in advance.

推荐答案

这里有一个替代方案,其中结果变量的名称在子脚本调用期间在主脚本中定义,而不是由子脚本本身定义:

Here is an alternative where the name of the resulting variable becomes defined in the main script during the call of the sub-script rather than by the sub-script itself:

foo.bat:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define name of variable to receive result here:
call "%~dp0bar.bat" RESULT 2 3
rem // Return result:
echo Result: %RESULT%
endlocal
exit /B

bar.bat:

@echo off
setlocal DisableDelayedExpansion
set "INT1=%~2"
set "INT2=%~3"
set /A "SUM=INT1+INT2"
rem // Assign variable that has been defined during the call:
endlocal & set "%~1=%SUM%"
exit /B

这篇关于如何将参数传递给另一个正在运行的批处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆