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

Joey
  • 344,408
  • 85
  • 689
  • 683
Matt
  • 21,026
  • 18
  • 63
  • 115
  • 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 '12 at 21:02
  • +1 for using `type` such a cool and simple solution. – Dominic P Mar 01 '13 at 01:34
  • @DominicP except that it does not work for my purposes (read after the "EDIT:") – Matt Mar 07 '13 at 01: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 07 '13 at 20:49

4 Answers4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark K Cowan
  • 1,755
  • 1
  • 20
  • 28
  • 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! – Mark K Cowan Jul 24 '13 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! – Mark K Cowan Jul 31 '13 at 15:34
  • Ah how could I ever have forgotten to mention Cygwin on this dual-boot machine? :D – Mark K Cowan Jul 31 '13 at 15:40
  • Also, `COPY CON output.txt` – Patrick McDonald Jun 09 '17 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 07 '17 at 09:37
  • On win10 you need a little C program looping fgetc/fputc – Mark K Cowan Aug 07 '17 at 09:39
  • @robert4: What's the difference between stdin & console? – Zimba Nov 05 '19 at 14:38
  • 1
    @Zimba: [See this question](https://stackoverflow.com/q/6447947/1348138), 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 '19 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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 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 '12 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 – Robert Calhoun Nov 30 '12 at 17:26
  • @dbenham - `findstr "^" < %windir%\system32\calc.exe > c.exe` gives `Line is too long` error on Win7. Not suitable for binary copying? – Egor Skriptunoff Aug 15 '18 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 https://stackoverflow.com/a/8844873/1012053 for more info. – dbenham Aug 15 '18 at 19:37
  • @EgorSkriptunoff - I've updated the answer to include the line length limitation. – dbenham Aug 15 '18 at 20:17
  • @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? – Egor Skriptunoff Aug 16 '18 at 07:44
  • @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. – dbenham Aug 16 '18 at 12:42
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!)

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
0

Standard input:

Batch file:

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

set /p MyVar=

echo %MyVar% > filename
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JMC
  • 910
  • 7
  • 11
  • 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 '12 at 21:08