20

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).

4
  • 1
    type thisfile > output.txt pushes all output same as unix cat type thisfile >> output.txt appends all output to file without overwriting
    – Kyle
    May 30, 2012 at 21:02
  • +1 for using type such a cool and simple solution.
    – Dominic P
    Mar 1, 2013 at 1:34
  • @DominicP except that it does not work for my purposes (read after the "EDIT:")
    – Matt
    Mar 7, 2013 at 1:12
  • @Matt, yeah it's not quite the same. I'm not sure how much you've worked with the Windows shell, but finding such a close approximation to *nix functionality is pretty rare/nice in my experience. :)
    – Dominic P
    Mar 7, 2013 at 20:49

4 Answers 4

26

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).

8
  • 6
    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! Jul 24, 2013 at 13:26
  • 1
    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! Jul 31, 2013 at 15:34
  • 1
    Also, COPY CON output.txt Jun 9, 2017 at 11:32
  • 5
    -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.
    – robert4
    Aug 7, 2017 at 9:37
  • 1
    @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
    – robert4
    Nov 20, 2019 at 15:31
13

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.

7
  • EDIT - Corrected info about piped input having CR/LF appended. Added info about XP and Windows 7 potentially hanging with redirected input
    – dbenham
    Nov 29, 2012 at 17:32
  • 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 Nov 30, 2012 at 17:26
  • @dbenham - findstr "^" < %windir%\system32\calc.exe > c.exe gives Line is too long error on Win7. Not suitable for binary copying? Aug 15, 2018 at 16:57
  • 1
    @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
    Aug 15, 2018 at 19:37
  • @EgorSkriptunoff - I've updated the answer to include the line length limitation.
    – dbenham
    Aug 15, 2018 at 20:17
10

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!)

2
  • Actually this one helped when i had mutiple lines.Thank you Oct 22, 2017 at 16:54
  • Nice. This is the only one that worked with stdin. The other solutions only piped con or from another file.
    – nharrer
    Apr 7, 2021 at 13:43
0

Standard input:

Batch file:

:: set /p MyVar=   Prompts user for stdin information

set /p MyVar=

echo %MyVar% > filename
1
  • 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.
    – Matt
    May 30, 2012 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.