I am somewhat new to using the command-prompt for programming related purposes so if this makes no sense or seems unclear please request that I elaborate and I will do my best.
What I am hoping to be able to do is have two command-prompt windows open on my windows machine (running Windows 7) and have them communicate with each other. For instance I want Cmd window A to tell Cmd window B to print the contents of a file and vice versa.
Is this in anyway possible? Also, I know there may be a way to do this by polling a file in one Command-prompt window, but I would like an asynchronous/non-blocking way of doing this if it is possible.
Thanks in advance!
Here's a way...
Starting with an empty directory (U:\comm), you can
start
any number of instances of the batch, each with a unique name as its first parameter.Establish a file called instancename.run to flag that a routine is running.
The caper with "randmod" is designed to modify the return of
%random%
by recording the hundredths-of-a-second. Two batch files starting at "the same" time will generate the same sequence of random numbers, but the hundredths-of-a-second will likely be different. To supplement this, I'd invoke the above asIn a loop controlled by the
ping
timeout, we then see whether the flagfile "quit" exists and if it does, delete theinstancename.run
file (and quit)Otherwise, look for other names that are running (*.run files) and set
others
to a list of those instancenames. If it changes from the previous version, report the change (this is solely for demo purposes, of course.)Then select a random number 0..99, modifying the returned random to get over synchronisation (it's quick and dirty for a demonstration. Doesn't need to be exact...)
If the chosen number is 37 then send a "count" insruction to another instance, else if it's 51, send a "repeat" instruction (just for demo - 1% chance of each)
The
instruct
routine chooses a destination from the list of other instances and builds an instruction (count/repeat + a random number) which is recorded in a file called thisinstancename.destinationinstanceThen we check to see whether we've received an instruction and if we have, we execute it.
The two routines
count
andrepeat
simply show different things in the destination instance - all for demonstration purposes.The selection is echoed for security-blanket purposes - showing that the loop's still running and displaying a significant number controlling the destination process.
The only way of interaction of 2 cmd windows is to call one from another from a batch file. You can pass variables from one to another.
E.g. you can set a variable in one batch and then call the second one from the first passing a variable as an input parameter. here is an example when I determine SID value for user user first and then pass it to the script for running it to patch the computer remotely.
What I have in the second batch that I run from the first one is:
So I pass %sid% from first batch and second one accepts it via set sid=%1.
The second tip is to store some variable to a temp or txt file in one batch and to read from it in another batch.
This code stores a set of registry subkeys from a specific key to reg.txt. Then we read this file line by line in for /l cycle.
I found it quite simple to make two command windows interact with each other.
Batch file 1:
Batch file 2:
Add in a counter on it and i would guess you could make it work in a longer code that interacts with each other.
If you want it to work over your network then use poshd/popd