Original batch file cmd window to other window jump

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rbbr000
Posts: 1
Joined: 05 Jul 2017 04:35

Original batch file cmd window to other window jump

#1 Post by rbbr000 » 05 Jul 2017 04:55

Hi,
I want to write a batch file to operate another cmd prompt.
Can anyone tell how to jump control from one cmd to another such that the later lines in the batch file are executed in the other command prompt window.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Original batch file cmd window to other window jump

#2 Post by ShadowThief » 05 Jul 2017 19:14

Not sure why you'd want to do this, but you'd have to make separate sender and listener scripts that share a common file with the sender writing to it and the receiver reading from it every few seconds.

I could have sworn I posted something like this here a while ago but I can't seem to find it...

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Original batch file cmd window to other window jump

#3 Post by PaperTronics » 05 Jul 2017 21:52

This is extremely easy to do! :)

You'll have to make a datafile like : MyDataFile.Data
and then use echo to write the data in the datafile and then use For /f to extract the data from the file.

Original Batch File:

Code: Select all

@echo off 
Setlocal EnableDelayedExpansion
Set Data=MyDataFile.data
>!Data! (
   Echo SuperImportantData
   Echo EvenMoreSuperImportantData
   Echo SuperSuperSuperSuperImportantData
   )


Batch File That receives the super important data :

Code: Select all

@echo off 
Setlocal EnableDelayedExpansion
Set Data=MyDataFile.data
For /f "tokens=*" %%A IN (!Data!) DO (
   Set FirstLine=%%A
   Set SecondLine=%%B
   Set ThirdLine=%%C
   ::And so on!
   )



Hope I helped!

npocmaka_
Posts: 513
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Original batch file cmd window to other window jump

#4 Post by npocmaka_ » 06 Jul 2017 05:16

you can use AppActivate function from windows shell application object from jscript or vbscript or powershell.

einstein1969
Expert
Posts: 947
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Original batch file cmd window to other window jump

#5 Post by einstein1969 » 06 Jul 2017 17:36

this is an approch:

Open two command prompts in the same current directory,

On the server side,

Code: Select all

Call >0
Tail-f 0 | cmd 1>1 2>2


On the client side,

Code: Select all

Start /b tail-f 1
Start /b tail-f 2
Copy /y con 0

When you enter a command here, it is executed on the server side and the result is displayed on the client side.

einstein1969

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Original batch file cmd window to other window jump

#6 Post by dbenham » 07 Jul 2017 09:59

@einstein1969 - What tail :?:
I tried with gnu tail for Windows, and could not reproduce. I don't understand why you are having the server output appear on the client - I don't get that from the OP's request at all.

@rbbr000 - I've demonstrated how this can be done with pure batch at Send commands to a cmd window through a .bat file - Rejected StackOverflow question


Dave Benham

einstein1969
Expert
Posts: 947
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Original batch file cmd window to other window jump

#7 Post by einstein1969 » 07 Jul 2017 11:32

@Dave

for my experiment I have used a jscript implementation of tail -f.

tail-f.cmd

Code: Select all

@if(0)==(0) ECHO OFF
CScript.exe //NoLogo //E:JScript "%~f0" %1
GOTO :EOF
@end
var StdIn=new ActiveXObject('Scripting.FileSystemObject').OpenTextFile(WScript.Arguments.Item(0));
while(true) if(StdIn.AtEndOfStream) WScript.Sleep(1000);else WScript.StdOut.Write(StdIn.Read(4096));


the step are:

- open cmd window 1

Code: Select all

call >0
copy /y con 0


-open cmd window 2

Code: Select all

tail-f 0 | cmd


This for output on window 2. But if windows 2 is started via taskscheduler (ie for execute privileged operation) the output may be redirected back to window 1 and then required 2 backgroung tail-f (look previuos post).

Onother implementation that I found for tail -f is this but there is a problem for order of startup for the version on previous post. Work well for this example.

tail-f.cmd

Code: Select all

@ECHO OFF
SETLOCAL
IF NOT EXIST "%~1" (
ECHO Usage: %~nx0 file
GOTO :EOF
)
SET SS=0
SET LL=0
:TOP
IF %SS%==%~z1 (
rem SLEEP 1
ping localhost -n 2 >NUL
CMD /C "TYPE NUL>>%1" 2>NUL
IF NOT ERRORLEVEL 1 GOTO :EOF
GOTO :TOP
)
SET SS=%~z1
FOR /F "delims=[] tokens=1*" %%1 IN ('FIND /N /V "" ^<%1 ^|MORE +%%LL%%') DO (
SET LL=%%1
ECHO:%%2
)
GOTO :TOP


windows7

Einstein1969

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Original batch file cmd window to other window jump

#8 Post by dbenham » 07 Jul 2017 13:16

Thanks.

My gnu version of tail does not work well because it buffers the output - nothing is written until the output buffer is full :(

Your strategy is basically the same as what I did in my linked post, except I am effectively implementing a pure batch version of tail -f


Dave Benham

Post Reply