How do you redirect standard input to a file in the Windows command line?

36,329

Solution 1

TYPE CON

CON is the MS-DOS device for console input. You can redirect to a file as follows:

TYPE CON>output.txt

To terminate, hit Ctrl + C or Ctrl + Z, Enter (Ctrl + Z = EOF).

Solution 2

If all you want is to read stdin and write what you read to stdout, then FINDSTR may work, depending on how you use it.

FINDSTR will output an exact binary image of the input as long as the input is specified as a single file name at the end of the argument list.

findstr "^" file.txt

Pipes or redirection may also work, depending on the content of the input:

findstr "^" < file.txt
or
type file.txt | findstr "^"

The output will be corrupted if any of the following occur while using redirected or piped input with FINDSTR:

  • Any input line > 8191 bytes
  • Last line of input is not terminated by \n. (command may hang if redirected input)

FINDSTR will not work if multiple input files are specified because in that case the name of the file will be used as a prefix to each line of output.

FINDSTR also differs from cat in that it cannot read from both stdin and a named file.

See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

Solution 3

I think more.exe might be what you are looking for assuming you are working with text data (this will break binary data).

It can take input both from the console:

more > file1.txt

Or piped in from another file, which TYPE CON doesn't handle:

type file1.txt | more > file2.txt

(more seems to append a newline to your file and expands tabs, so don't use it on binary data!)

Share:
36,329
Matt
Author by

Matt

If I'm giving you a hard time, it's because I'm trying to learn as much as I can from you.

Updated on August 16, 2020

Comments

  • Matt
    Matt almost 4 years

    On Unix I would do something like:

    cat > file.txt
    

    How can I do this on the Windows command prompt or batch file?

    EDIT: Basically, I am looking for the functionality of cat with no arguments (it reads from stdin and spits it back out to stdout).

  • Matt
    Matt almost 12 years
    This would have to read the entirety of stdin first though, wouldn't it? It seems like it would be impractical if stdin is very large.
  • dbenham
    dbenham over 11 years
    EDIT - Corrected info about piped input having CR/LF appended. Added info about XP and Windows 7 potentially hanging with redirected input
  • Robert Calhoun
    Robert Calhoun over 11 years
    Thank you for this answer! For anyone else out there, a batch file that writes (appends) stdin to file "foo.txt" is: findstr "^" >>foo.txt
  • Mark K Cowan
    Mark K Cowan almost 11 years
    Ah lol! It might be useful for others though I guess... I'm probably the only person my age who knows of the elegant TYPE CON>LPT1 shortcut to turn MS-DOS into a typewriter!
  • Mark K Cowan
    Mark K Cowan almost 11 years
    Windows alternatives to many *nix commands do exist - but they "evolved" rather than being intelligently designed as bash was, so they're a bit harder to find and often far more complex to use than they should be... A great example of this difference is the bash backtick vs. the equivalent FOR construct in DOS. Thank heavens for Powershell and C#script!
  • Mark K Cowan
    Mark K Cowan almost 11 years
    Ah how could I ever have forgotten to mention Cygwin on this dual-boot machine? :D
  • Patrick McDonald
    Patrick McDonald almost 7 years
    Also, COPY CON output.txt
  • robert4
    robert4 almost 7 years
    -1 because it doesn't work: it does not read from stdin but from the console. The OP asked for the stdin. I tested it on win10.
  • Mark K Cowan
    Mark K Cowan almost 7 years
    On win10 you need a little C program looping fgetc/fputc
  • TheGameiswar
    TheGameiswar over 6 years
    Actually this one helped when i had mutiple lines.Thank you
  • Egor   Skriptunoff
    Egor Skriptunoff almost 6 years
    @dbenham - findstr "^" < %windir%\system32\calc.exe > c.exe gives Line is too long error on Win7. Not suitable for binary copying?
  • dbenham
    dbenham almost 6 years
    @EgorSkriptunoff - Pipes and redirection limit FINDSTR input line length. Specify the input file as an argument (simply remove the <) rather than use redirection, and it should work. See Line Length limits section at stackoverflow.com/a/8844873/1012053 for more info.
  • dbenham
    dbenham almost 6 years
    @EgorSkriptunoff - I've updated the answer to include the line length limitation.
  • Egor   Skriptunoff
    Egor Skriptunoff almost 6 years
    @dbenham - Thanks. Actually, I'm trying to find how could I copy binary data from ADS to separate file, I was hoping the following would work: findstr "^" < file.txt:hidden.exe > hidden.exe. Unfortunately, findstr doesn't consider ADS as a valid file name: findstr "^" file.txt:hidden.exe > hidden.exe (error: Can't open file "file.txt:hidden.exe"). type doesn't work either. So I have to use redirection < to access ADS. I'm searching for a Win7 utility that reads binary stdin and writes it to stdout (or to an output file). Didn't find it yet. Could you give a hint?
  • dbenham
    dbenham almost 6 years
    @EgorSkriptunoff - Your issue deserves its own question - you should post one. I'm not aware of any native batch utilities that will work. I suspect something could be written with another scripting language, especially powershell. But I haven't much experience with ADS, so I'm not sure.
  • Zimba
    Zimba over 4 years
    @robert4: What's the difference between stdin & console?
  • robert4
    robert4 over 4 years
    @Zimba: See this question, or the difference between echo hello | find "hello" >first.txt and echo hello | type con >second.txt. first.txt will contain hello without asking for typing, but for second.txt you have to type in something
  • nharrer
    nharrer about 3 years
    Nice. This is the only one that worked with stdin. The other solutions only piped con or from another file.