Best way to transfer data between two windows

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Best way to transfer data between two windows

#1 Post by Sounak@9434 » 03 Mar 2017 08:23

Well, my target is simple.
I have one window for input and another window for output.
In one window you can type text which will be sent to the other window for analysis.
Till now I'm using this script.

Code: Select all

::Window one
@echo off
setlocal enabledelayedexpansion
for /l %%a in () do (
   set /p input=Input.
   echo(!input!>data.txt
)

Code: Select all

::Window two
@echo off
setlocal enabledelayedexpansion
for /l %%a in () do (
   if exist data.txt (
      set /p data=<data.txt
      del data.txt
      echo(!data!
   )
)

This works fine but is there any better way like using pipe(|) or anything else?
Hoping for help.

Sounak

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Best way to transfer data between two windows

#2 Post by Aacini » 03 Mar 2017 10:51

This works here:

Code: Select all

::Window one
@echo off
setlocal enabledelayedexpansion
ping -n 2 localhost > NUL
for /l %%a in () do (
   set /p input=Input.
   if errorlevel 1 exit
   echo(!input!
)


Code: Select all

::Window two
@echo off
setlocal enabledelayedexpansion
for /l %%a in () do (
   set /p data=
   if errorlevel 1 exit
   echo(!data!
)

Start it this way:

Code: Select all

window1.bat | window2.bat

Antonio

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Best way to transfer data between two windows

#3 Post by Sounak@9434 » 03 Mar 2017 11:42

@Aacini: It works fine but there are not two windows. Any ways :?:

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Best way to transfer data between two windows

#4 Post by Sounak@9434 » 03 Mar 2017 21:09

Making it more clear, There should be two separate cmd windows.
One would take data through 'set /p' and sent the input string two the other cmd window. It would receive the string and show it on screen.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Best way to transfer data between two windows

#5 Post by aGerman » 04 Mar 2017 15:36

Two batch windows does always mean you need two processes (because it's not possible to attach more than one console window to a process). Having a data file as interface between two batch processes is the common way. If you worry about data races (accessing the data from both processes at the same time) you need to signal if you are ready with writing or with reading. There are different ways. The easiest (imho) is to have a second file that will be created after the data was written and will be deleted after the data was read. E.g.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
if "%~1"=="two" goto two
2>nul del "signal~"
start cmd /c "%~f0" two

title Window one
for /l %%i in () do if not exist "signal~" (
  set "input="
  set /p "input=Input: "
  >"data~" echo(!input!
  >"signal~" type nul
)


:two
title Window two
for /l %%i in () do if exist "signal~" (
  set "data="
  <"data~" set /p "data="
  echo(!data!
  del "data~"
  del "signal~"
) else (
  >nul ping -n 1 -w 100 192.0.2.0
)

Steffen

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Best way to transfer data between two windows

#6 Post by Sounak@9434 » 04 Mar 2017 20:49

Just two questions here.
1) According to the 'cmd /?', the output of cmd command can be piped using /a or /u switch. True?
2) In Antonio's answer one script is using echo and the other is setting it up through pipe to set /p. So how come that solution work and 'echo abcd|set /p text=' Fails?

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Best way to transfer data between two windows

#7 Post by penpen » 05 Mar 2017 03:15

Ad 1) Yes, this is true.
Ad 2) The compound command 'echo abcd|set /p text=' doesn't fail. The subprocesses just end after that command losing the result:

Code: Select all

Z:\>echo abcd|(set /p "text=" & set text) & set text
text=abcd
Die Umgebungsvariable "text" ist nicht definiert.

Z:\>


penpen

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Best way to transfer data between two windows

#8 Post by Sounak@9434 » 05 Mar 2017 06:45

@Penpen: Thanks penpen. That helped in solving some of my old problems :)

Post Reply