Matt
Matt

Reputation: 22133

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

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

Upvotes: 21

Views: 49323

Answers (4)

Carl Walsh
Carl Walsh

Reputation: 6989

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

Upvotes: 10

dbenham
dbenham

Reputation: 130899

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.

Upvotes: 13

JMC
JMC

Reputation: 930

Standard input:

Batch file:

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

set /p MyVar=

echo %MyVar% > filename

Upvotes: 0

Mark K Cowan
Mark K Cowan

Reputation: 1863

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

Upvotes: 27

Related Questions