BlueLizards
BlueLizards

Reputation: 15

Batch send echo to another batch

I've been searching for a while to find a way to do this properly but I haven't been able to. I was wondering how it would be possible to have two batch files communicate with each other. The idea is trying to make a live console, instead of one batch file doing calculations printing the results in a .txt file I would like to see them show up live on another batch file open next to it. Any idea how this would be possible? So far I've been using

set logfile="Files\Storage\log.txt"

set logg=^> _^& type _^&type _^>^>%logfile%

and then adding %logg% at the end of each echo I want logged like so:

echo Hello world %logg%

I was wondering if there was a way to send this to another batch file using %success% and make it print on this second batch as an echo. So for example:

Batch 1:

@echo off
title Batch 1
pause
REM so it says press any key to continue, and once any key is pressed:
echo Hello World %success%
pause
echo How are you today Stack Overflow %success%
pause 
exit

and somehow Batch 2 looks like:

after the first pause:

Hello World

after the second pause:

Hello World
How are you today Stack Overflow

and batch 2 keeps refreshing and adding lines as they come (maybe this can be done using a loop that checks for input from the other batch?)

I'm not sure exactly how to go about doing this, especially the second batch's code. I've tried using

echo echo Hello World > batch2.bat

But this erases the contents of the script of batch2 and replaces all the code with "echo Hello World" and does NOT print it as an echo on the file as it deletes @echo off and any loop that was keeping batch2 open.

Any help would be greatly appreciated!

~Thanks in advance :)

Upvotes: 1

Views: 1571

Answers (1)

Aacini
Aacini

Reputation: 67216

The code below do what you want, but you should realize that there are a couple synchronization details that must be solved: 1. You must select the window of Batch1 before press the key for first pause command, and 2. The Batch1 must notify Batch2 that it should terminate; otherwise, Batch2 will never ends.

This is Batch1.bat:

@echo off
title Batch 1

rem Create the communication file
copy NUL log.txt > NUL

rem Define the send variable
set "success=>> log.txt"

rem Activate Batch 2 as receiver
start Batch2 ^< log.txt


pause
REM so it says press any key to continue, and once any key is pressed:
echo Hello World %success%
pause
echo How are you today Stack Overflow %success%
pause 
rem Send to Batch2 the signal to terminate
echo exit%success%
exit /B

This is Batch2.bat:

@echo off

:inputLoop
   set "input="
   set /P "input="
   if not defined input goto inputLoop
   if "%input%" equ "exit" exit
   echo %input%
goto inputLoop

Upvotes: 1

Related Questions