12

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!

foxidrive
  • 40,353
  • 10
  • 53
  • 68
Helen
  • 255
  • 2
  • 3
  • 8
  • I'm assuming you're trying to do this without an external file with input? – lc. Sep 24 '13 at 02:48
  • yeah. no external file. That 3 numbers are supposed to be fixed after the user first sets up the batch file – Helen Sep 24 '13 at 02:51

4 Answers4

14

This may also work:

(
echo 2
echo 3
echo 8
) | mycode.exe
foxidrive
  • 40,353
  • 10
  • 53
  • 68
5

try this:

run.bat:

myCode.exe %1 %2 %3

call example:

run.bat 111 222 333

and with file:

run.bat < input.txt

msangel
  • 9,895
  • 3
  • 50
  • 69
1

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.

Vladimir Sinenko
  • 4,629
  • 1
  • 27
  • 39
  • Now the code is in C# and is using Console.readLine() to get input. I may need to add newline between 2 and 3. – Helen Sep 24 '13 at 02:54
  • Take a look [here](http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files) for embedding a newline constants in a batch file. You'll just probably need three echo statements on a line. – Vladimir Sinenko Sep 24 '13 at 02:56
  • echo 9 && echo. && echo 19 && echo. $$ echo 2 > output | myCode.exe output i ll try if this works. Thanks Vladimir! – Helen Sep 24 '13 at 03:28
  • 2
    It's standard practice to separate commands with an ampersand `command1 & command2` as a pipe will confuse newbies and teach them false practices. – foxidrive Sep 24 '13 at 03:29
  • @foxidrive, absolutely agreed. Edited the answer accordingly. – Vladimir Sinenko Sep 24 '13 at 03:52
  • 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. – dbenham Sep 24 '13 at 11:08
0

try type input.txt | myCode.exe

zxch3n
  • 387
  • 3
  • 9
  • `cat` is not a native `cmd` command, but a unix command (the command `type` does basically the same, but with fewer options). – Stephan Oct 11 '18 at 15:00