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

后端 未结 4 1065
一个人的身影
一个人的身影 2021-01-01 19:34

On Unix I would do something like:

cat > file.txt

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

EDIT:

相关标签:
4条回答
  • 2021-01-01 20:11

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

    0 讨论(0)
  • 2021-01-01 20:14

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

    0 讨论(0)
  • 2021-01-01 20:20

    Standard input:

    Batch file:

    :: set /p MyVar=   Prompts user for stdin information
    
    set /p MyVar=
    
    echo %MyVar% > filename
    
    0 讨论(0)
  • 2021-01-01 20:26

    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.

    0 讨论(0)
提交回复
热议问题