9

In Windows I have console programs that run in the background with the console hidden. Is there anyway to direct input to the programs console? I want to be able to do something like:

echo Y| *the_running_process_here*

to send Y to the process' stdin.

phuclv
  • 37,963
  • 15
  • 156
  • 475
myforwik
  • 195
  • 1
  • 2
  • 6
  • Doing it with console may be a tough ask, but an option like a wrapper python script, which launches the console program as a child process. It can listen to a socket or something and then pass the command to stdin of the console program? – Tarun Lalwani Aug 25 '19 at 09:32
  • @TarunLalwani - I guess that's why it never got an answer. I would like a definitive, "It can't be done because..." answer if there is no "here's how to do it..." answer. – Morag Hughson Aug 28 '19 at 08:35
  • Is the process already running, or should it run as part of the command? – Kfir Dadosh Aug 30 '19 at 16:24
  • Hi @KfirDadosh - the process is already running – Morag Hughson Sep 02 '19 at 03:27

1 Answers1

2

As far as I know this is not possible by basic cmd commands. PowerShell is more powerful though and can use the .NET framework from windows.

I found this PowerShell script which claims to pass text to an already opened cmd:

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "cmd.exe"; #process file
$psi.UseShellExecute = $false; #start the process from it's own executable file
$psi.RedirectStandardInput = $true; #enable the process to read from standard input

$p = [System.Diagnostics.Process]::Start($psi);

Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running

$p.StandardInput.WriteLine("dir"); #StandardInput property of the Process is a .NET StreamWriter object

Source: https://stackoverflow.com/a/16100200/10588376

You would have to safe this script with an .ps1 ending and run it.

A workaround to use this as a cmd command would be to make it accept arguments, so that you can run it with yourScript.ps1 procced_pid arguments for example.

The standard windows cmd is just too limited to fullfill such a task by its own.

MarvinJWendt
  • 2,357
  • 1
  • 10
  • 36
  • I'm not sure I fully understand what this script is doing? If the process I want to send stdin to is already running, how does this script know which process I mean? – Morag Hughson Aug 28 '19 at 23:57