-2

I would like to start a subscript to calculate something for me and return the value somehow if it's done.

foo.bat:

@echo off
start bar.bat 2 3
::This foo.bat does something and after a while it waits for the calculated value.
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

I tried to ask this question here but I got redirected by three people to the call function, which is not what I need (in my opinion).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    The key is you want to do it "parallel" (while your `foo.bat` does something "useful"). That means you have two processes that can't communicate (by design) with each other. Your best option is to use a file. – Stephan Jun 15 '20 at 15:00
  • well that's unfortunate if that's the case. But how do games or other programs do it. I bet they aren't making a file so the threads can communicate... – Boldizsár Németh Jun 15 '20 at 15:09
  • 1
    they don't use `cmd.exe`... – Stephan Jun 15 '20 at 15:22
  • See [my answer here for an example of the basics of running multiple threads within a batch file](https://stackoverflow.com/a/61697204/12343998) or [Dave benhams much more advanced snake.bat](https://www.dostips.com/forum/viewtopic.php?f=3&t=4741&hilit=snake.bat) – T3RR0R Jun 15 '20 at 16:04
  • Well, as long as you do not precisely describe what you want to achieve, no-one can really help you… – aschipfl Jun 15 '20 at 16:11
  • What if you just made one of the files a subroutine in the same file then called it then? – Nico Nekoru Jun 15 '20 at 17:49

1 Answers1

0

Try that :

Main batch file

CALL sub_batch_file.bat  return_here "Second parameter input"

REM echo is Second parameter input
ECHO %return_here%
REM End of main-batch file

sub_batch_file.bat

@ECHO OFF
SETLOCAL

REM ~ removes the " "
SET input=%~2
(
    ENDLOCAL
    SET %1=%input%
)
exit /b
REM End of sub-batch file
Jonathan Delean
  • 1,011
  • 1
  • 8
  • 25
  • This is exactly the opposite. I would like to use more then 1 threads for my project since it is way more complicated then this example. If I know it correctly this solution would run only from the caller cmd.exe. Which is not what I want. – Boldizsár Németh Jun 15 '20 at 14:59
  • So do you want a multithreading script who return value ? do you want they finish all before use all the result ? i dont really get why you want to do that, can you explain me, i will be able to give you more help – Jonathan Delean Jun 15 '20 at 15:02