11

I have a program P that processes stdin. How can I feed a file into this program from the Windows command line?

P < filename

does not work.

filename | P
filename > P

does not work either.

edit

tried again, in fact

P < filename

or

grep abc < lab.txt

does work.

bad_coder
  • 643
joh
  • 563
  • 1
    What OS are you working on? – EBGreen May 12 '14 at 19:58
  • 2
    Command redirection in MS Windows works just like in GNU/Linux. If p < myfile is not working, it probably is not a problem of the redirection. Try something like sort < myfile and see what happens. – bjanssen May 12 '14 at 20:16
  • as mentioned, p < file does not work. – joh May 12 '14 at 20:44
  • i can certainly write a program that reads the file and forwards it to stdout. i just wonder if such thing does exist already. – joh May 12 '14 at 20:44
  • which language are you using to make this program P ? – Mansueli May 12 '14 at 20:49
  • P is written in C#, .Net . – joh May 12 '14 at 20:50
  • Try to post your program in StackOverflow, I think it might be a problem in the program. Or maybe you forgot to specify the output: P.exe <input_filename.txt >output_filename.txt (note that even if the filenames are hidden you still must use them for passing the names) – Mansueli May 12 '14 at 20:57
  • my program is correct, please do not doubt that. the behavior is the same when I exchange P with grep for instance. – joh May 12 '14 at 21:05
  • 1
    Have you tried type filename | P – Kevin Fegan May 12 '14 at 21:18
  • that works also, interesting, thx! – joh May 12 '14 at 21:28
  • 2
    Does that solve your problem? Should I post that as an answer? ... Wait, I see you edited your question that P < filename is now working. Sounds like you no longer have a problem. – Kevin Fegan May 12 '14 at 21:30
  • yes, feel free to do so, your solution is correct. actually the hints from bjanssen was also correct, since P < file in fact works. – joh May 12 '14 at 21:34
  • Beware, using type seems to sometimes "eat" newlines in the input (if the newline is on an internal buffer boundary, I suspect -- rather nasty to get 42 where 4\n2 is in the file); the approach with < (https://stackoverflow.com/a/15991395/110118) seems to work nice though – mlvljr Aug 14 '19 at 20:42

1 Answers1

2

I tried this script that set the contents of the filename as variable "a" and then fed this variable "a" as an argument to the "p" program.

@echo off
for /f "tokens=1 delims={" %%a in ('TYPE filename ^| FINDstr /r "[a-z]"') do (cmd /c p "%%a")
pause

Note that this script has a number of limitations as I used regular expression in findstr command supposing that the contents of filename is just text. I don't know what your filename contents are so you have to modify it to suites your needs. Also you need to modify your program "p" so that it accepts arguments. Hope this help.