0

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.

  • 3
    Use `call` to run a script from another script. Parent scripts can read the variables from child scripts as long as the child script doesn't use `setlocal`. – SomethingDark Jun 14 '20 at 08:37
  • I use that but i would like to call the function from a separate file. If its possible. So i don't have to attach all the useful functions to the end of my current project. Or does the call function work from separate files as well? – Boldizsár Németh Jun 14 '20 at 08:49
  • 1
    I'm not sure what you mean. Are you saying that you want foo.bat to call an intermediary script that calls bar.bat? – SomethingDark Jun 14 '20 at 08:53
  • No. I want to call bar.bat script (which I know how to do) from foo.bat and I want to return the value somehow to foo.bat (which I don't know how to do) – Boldizsár Németh Jun 14 '20 at 09:04
  • 2
    Then instead of `start bar.bat 1 2`, use `call bar.bat 1 2` and when `%result%` gets set in bar.bar, foo.bat will be able to see it automatically because the two scripts share the same environment. – SomethingDark Jun 14 '20 at 09:05
  • can you please write an example script for me. I kinda understand what you are saying but i don't want to spend an hour figuring it out completely. Thanks in advance. – Boldizsár Németh Jun 14 '20 at 09:08
  • 2
    Literally just change the line `start bar.bat 1 2` in foo.bat to `call bat.bat 1 2` and delete the `set result=//somehow return the value` line after that. – SomethingDark Jun 14 '20 at 09:09
  • Thanks it worked. But you forgot that I needed to modify the exit to exit /b as well. Thanks for the help. – Boldizsár Németh Jun 14 '20 at 09:23

2 Answers2

0

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
exit /B

: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.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and 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 /?
  • goto /?
  • pause /?
  • set /?
  • setlocal /?
  • start /? ... not used anymore

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99