We have a C# console application, which takes various input(note- no command line input)
Eg
C:\MyTool.exe
Enter Option (1-4):
Enter Location(1-7):
Enter Region (1-3):
After providing these input one by one, tool process the input and produce some business result.
Now, I have a requirement to run these tool every hour with Option -1, Location -2, Region-1
What I am planning to do is to setup the Windows Schedular, which will call these EXE, but the problem is how can I provide the input which in may case (1, 2, 1) respectively to the console tool.
I am thinking is there is any way that I can provide the input upfront in the command line itself...
Eg C:\MyTool.exe << 1 2 1 So that I can create batch file of these command and schedule it accordingly.
Please suggest.
Thanks,
Siraj
Create an input file where the input options are separated by CRLF (carriage return, Line feed). For example, open a command prompt and go to the folder where your MyTool.exe is located. Then type: 'copy con input.txt<enter>1<enter>2<enter>1<enter><F6>' This will create the input file. Now use 'MyTool < input.txt'. This will run your tool with accepting these input parameters. This command can be used in the task scheduler.
Related
I have a simple c# program that expects command line input (from Console.ReadLine()) when ran. There's a lot of inputs I have to provide, so I was wondering how I could automate this process. I currently have a shell script that attempts to do this, but it's not working.
#!/bin/sh
dotnet run #run the program
1 #input first argument (this failed so I tried echo 1 instead but no luck)
# <- rest of command line inputs on each line
Not really familiar with shell scripts, and I'm not fixed on this solution if you had another solution in mind. My OS is MacOS.
OK, so what I tried was this:
I made a little mcve-Console App: (dotnet 6)
var input = Console.ReadLine();
while( !string.IsNullOrEmpty(input) )
{
Console.WriteLine("User input: {0}", input);
input = Console.ReadLine();
}
Then I made a little input.txt
This is a Text.
This is another Text.
1
2
3
This is the final Text.
and finally run.sh as follows:
#!/bin/sh
dotnet run < "input.txt"
Output was
User input: This is a Text.
User input: This is another Text.
User input: 1
User input: 2
User input: 3
User input: This is the final Text.
/bin/sh is linked to dash shell in my case.
If in run.sh you replace "input.txt" with "$1", then you can call it with ./run.sh whateveryourinputfileis.txt to make a little more flexible.
I'm trying to make a call from one process to start another, supplying starting arguments as separate parameters in a ProcessStartInfo. The starting call uses a URL registered in Windows to find the second program, entered into FileName. I then add a string containing 4 parameters to Arguments. As I have understood it, the spaces in the string indicate the separation of the arguments.
Program 1
//Create starting information
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo() {
FileName = "urlHandle:",
Arguments = "/argA valueA /argB valueB"
};
//Start second program
System.Diagnostics.Process.Start(startInfo);
Program 2
//Print received arguments to a file
Environment.GetCommandLineArgs().ToList().ForEach(a => writer.WriteLine(a + ",\t"));
The second program starts as intended (meaning the URL is working), but the output is incomplete.
[path to .exe of second program],
urlHandle:,
It contains the path to the program, the string set as FileName , but everything put into Arguments is missing.
Does anybody have any ideas why the arguments disappear?
Note 1: If I would add the arguments into the FileName, I would receive them as one string. In order to trigger the behaviour I want in the second program, I must supply it with several parameters instead of one. I know this is possible from testing it manually from the terminal.
Note 2: I'm using .Net Framework, so trying ArgumentList from .Net Core is not an option.
After some more tests I have found the issue. The error does not lie in how I set up and use ProcessStartInfo, but rather in that I am using a custom URL protocol.
In the Windows registry one defines the number of parameters that can be sent via the URL call ("C:\[path to my .exe]" "%1"). The first argument is the Filename, and is as such the only thing sent via the protocol. In order to send more, one is required to add "%2", "%3", etc.
Note: The path itself becomes argument 0 in the receiving program, while the actual sent parameters start at argument 1 and beyond.
This seems trivial, but google only gives me powershell-related stuff and how to implement pipelines within programs.
I'm trying to consume input arguments from the (standard command processor cmd.exe for Windows 7, not powershell) command line pipeline on windows like 'more' does.
I'm trying to get this to work with two self-created .exe files:
randgen (creates a random number and writes it to the console)
wordwrite (the c# program that I want to consume this random number from the pipeline)
And using two common windows executables to test and model the behaviour:
echo (writes input to console)
more (displays output one screen at a time, can consume input from the pipeline)
Here's the current content of my Main function in wordwrite:
static void Main(string[] args)
{
string ouname = args[0];
DocWrite(ouname);
}
and the behaviour trying to naively pipe output from randgen into wordwrite:
D:\code>randgen | wordwrite
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at ADDoc.ADOUDoc.Main(String[] args)
(plus a popup telling me wordwrite.exe has stopped working)
Which is the behaviour of trying to consume a null array of arguments as input:
D:\code>wordwrite
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at ADDoc.ADOUDoc.Main(String[] args)
So the output of randgen isn't being read from the pipeline correctly as an argument to the wordwrite executable's args array (thanks #Peter Duniho for explaining the difference between stdin and how argument assignation works). Contrast that with the behaviour of the 'more' executable:
D:\code>randgen | more
56929227
That said, this 'pipeline-awareness' seems to be inherent to the 'more' executable itself, as another common windows executable, echo.exe, is just as pipeline-blind as wordwrite.exe:
D:\code>randgen | echo
ECHO is on.
D:\code>echo blah
blah
D:\code>echo
ECHO is on.
Here we can see that the attempt to use the pipeline is effectively the same as providing no arguments for echo.exe (contrasted in the middle with behaviour when an argument is provided).
Which suggests there is something I can add the the body of my Main function in wordwrite.exe to enable pipeline-consuming behaviour like the 'more' executable does. Does anyone know how this is done?
You can read data 'from the pipeline' (effectively, the stdin stream*) by using the Console.ReadLine() method, like so:
static void Main()
{
string ouname = Console.ReadLine();
DocWrite(ouname);
}
*The pipeline is a tool to direct the output of one process (stdout) to the input (stdin) of another one. So when you think of 'consuming input from the pipeline', think 'assign stdin to a variable that you then do stuff with'.
Reference:
C# Console receive input with pipe
So, I am making a hacking game, and in it you control a terminal. So far, I have done 'help' and 'cls' commands, that don't require parameters. But, for say a echo command, I need the computer to detect the keyword 'echo' and the rest of the text as what you want to 'echo'. How can I do this? Thanks for any help.
You can do something line this:
var input = Console.ReadLine(); //You might have this already in your code
if (input.StartsWith("echo "))
{
var text = input.Substring(5);
Console.WriteLine(text);
}
This checks to see if the input line starts with echo, and if it does, it prints to the console whatever is after the first 5 characters (which is the length of echo and one space)
So there is a program someone wrote (that I don't have access to) that was written in C#, in which when I open it, it brings up a command prompt, asks several questions, and then returns an output.
What I want to do it write a batch file to automate entering all the arguments manually but nothing has really worked for me thus far. I tried "program.exe arg1 arg2.." in the command prompt and reading about the windows commands (I checked out ss64) but nothing seems to work.
So to summarize what happens is:
1) I open the program (a .exe file) in the command prompt (or click on it) where it asks me to enter a value or filename
http://i.stack.imgur.com/bZsSi.png
2) I press enter to continue to the next question and the command asks me to answer another question (unless I finished the last one, in which case the program finishes executing and then closes).
http://i.stack.imgur.com/nqJ5M.png
Now, how would I go about making a batch file that enters SWAIN.dat, n, 1000, etc... automatically into this program? Again, I don't have access to the original program. i only know it was written in C#.
Thank you very much.
You could create a VB script -
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 100
WshShell.SendKeys "program.exe{ENTER}"
WScript.Sleep 100
WshShell.SendKeys "SWAIN.dat{ENTER}"
WScript.Sleep 100
WshShell.SendKeys "1000{ENTER}"
etc...
You may try this:
(
echo SWAIN.dat
echo n
echo 1000
echo etc...
) | program.exe