How do you redirect standard input to a file in th

2020-02-28 03:10发布

问题:

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

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



回答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.



回答3:

I think more.exe might be what you are looking for.

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



回答4:

Standard input:

Batch file:

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

set /p MyVar=

echo %MyVar% > filename