Page 1 of 1
Original batch file cmd window to other window jump
Posted: 05 Jul 2017 04:55
by rbbr000
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.
Re: Original batch file cmd window to other window jump
Posted: 05 Jul 2017 19:14
by ShadowThief
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...
Re: Original batch file cmd window to other window jump
Posted: 05 Jul 2017 21:52
by PaperTronics
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!
Re: Original batch file cmd window to other window jump
Posted: 06 Jul 2017 05:16
by npocmaka_
you can use AppActivate function from windows shell application object from jscript or vbscript or powershell.
Re: Original batch file cmd window to other window jump
Posted: 06 Jul 2017 17:36
by einstein1969
this is an approch:
Open two command prompts in the same current directory,
On the server side,
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
Re: Original batch file cmd window to other window jump
Posted: 07 Jul 2017 09:59
by dbenham
@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 questionDave Benham
Re: Original batch file cmd window to other window jump
Posted: 07 Jul 2017 11:32
by einstein1969
@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
-open cmd window 2
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
Re: Original batch file cmd window to other window jump
Posted: 07 Jul 2017 13:16
by dbenham
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