Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send stdin to a process from windows command prompt

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.

like image 654
myforwik Avatar asked May 06 '10 06:05

myforwik


People also ask

How to redirect file in cmd?

On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands. Redirection can be done by using the operators > and >> .

How do I use YES in CMD?

Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.


1 Answers

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.

like image 113
MarvinJWendt Avatar answered Sep 20 '22 08:09

MarvinJWendt