how to pass input to .exe in batch file?

40,948

Solution 1

This may also work:

(
echo 2
echo 3
echo 8
) | mycode.exe

Solution 2

try this:

run.bat:

myCode.exe %1 %2 %3

call example:

run.bat 111 222 333

and with file:

run.bat < input.txt

Solution 3

Here is a batch one-liner that will create the file for you and supply it as an input to the myCode.exe:

echo 2 3 8 > output & myCode.exe output

Otherwise, you'll probably need to modify your program to read the arguments directly from command line.

It's possible to redirect the program standard input/output/error streams to or from a file, but I think there is no way to redirect a command line contents to a standard input stream. Take a look at this page for details on batch redirection.

Share:
40,948

Related videos on Youtube

Helen
Author by

Helen

Updated on July 05, 2022

Comments

  • Helen
    Helen almost 2 years

    I have a .exe which requires 3 integers as input. For example:

    myCode.exe < input.txt
    

    In input.txt:

    2
    3
    8
    

    Now I want to put the command in a batch file. how can I write the batch file? (Here I want to pass 3 fixed integers in the batch file)

    THANKS!

  • Helen
    Helen over 10 years
    Now the code is in C# and is using Console.readLine() to get input. I may need to add newline between 2 and 3.
  • Vladimir Sinenko
    Vladimir Sinenko over 10 years
    Take a look here for embedding a newline constants in a batch file. You'll just probably need three echo statements on a line.
  • Helen
    Helen over 10 years
    echo 9 && echo. && echo 19 && echo. $$ echo 2 > output | myCode.exe output i ll try if this works. Thanks Vladimir!
  • foxidrive
    foxidrive over 10 years
    It's standard practice to separate commands with an ampersand command1 & command2 as a pipe will confuse newbies and teach them false practices.
  • Vladimir Sinenko
    Vladimir Sinenko over 10 years
    @foxidrive, absolutely agreed. Edited the answer accordingly.
  • dbenham
    dbenham over 10 years
    +1, But based on way question is phrased, the OP may need the simpler echo 1 2 3|mycode.exe
  • foxidrive
    foxidrive over 10 years
    @dbenham I wondered about that and just edited the question - they were indeed on separate lines.
  • dbenham
    dbenham over 10 years
    The OP wants to avoid a temporary file, and the code will not work with the name of the temp file passed as an argument; it must be used as redirected input.
  • Stephan
    Stephan over 5 years
    cat is not a native cmd command, but a unix command (the command type does basically the same, but with fewer options).

Related