How do I pass parameters to a running batch program by another one

我怕爱的太早我们不能终老 提交于 2020-06-29 03:39:28

问题


example:

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.


回答1:


The batch file foo.bat must be written as follows:

@echo off
call "%~dp0bar.bat" 2 3
echo Result is: %result%
pause

The batch file bar.bat must be written as follows:

@echo off
set "int1=%~1"
set "int2=%~2"
set /A result=int1 + int2

The batch file foo.bat calls batch file bar.bat which means after processing of bar.bat the Windows command processor continues processing foo.bat with next command in that batch file after command CALL. Both batch files are processed by same cmd.exe and share the same list of environment variables.

That is not the case on using start bar.bat as this results in starting one more cmd.exe with its own list of environment variables for processing the batch file bar.bat being executed parallel by second instance of cmd.exe while first instance of cmd.exe continues processing foo.bat immediately after starting second cmd.exe.

The batch file foo.bat could be also written as:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
call :DoMath 2 3
echo Result is: %result%
echo/
pause
goto :EOF

:DoMath
set "int1=%~1"
set "int2=%~2"
set /A result=int1 + int2
goto :EOF

Everything below DoMath is now a subroutine which can be called multiple times from main code of foo.bat.

It is important that the batch files do not contain exit without parameter /B which results always in exiting cmd.exe processing the batch independent on calling hierarchy and independent on how cmd.exe was started before executing the batch file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains also %~dp0 ... drive and path of argument 0 which is the full path of the batch file always ending with a backslash.
  • echo /?
  • exit /? ... not used anymore
  • goto /?
  • pause /?
  • set /?
  • setlocal /?
  • start /? ... not used anymore

See also:

  • Debugging a batch file.
  • Where does GOTO :EOF return to?
  • How to call a batch file that is one level up from the current directory?
  • Why is no string output with 'echo %var%' after using 'set var = text' on command line?
  • What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?



回答2:


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


来源:https://stackoverflow.com/questions/62369942/how-do-i-pass-parameters-to-a-running-batch-program-by-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!