DOS CMD redirection operation

Source: Internet
Author: User
Tags access properties stdin
The command redirection operator allows you to use the redirect operator to reset the command input and output data streams from the default location
to a different location. The position of the input or output data stream is the handle.

The following table lists the available handles.

The numerical code description of handle handle
STDIN 0 Keyboard input
STDOUT 1 output to the command Prompt window
STDERR 2 Error output to command Prompt window
UNDEFINED 3-9 These handles are defined separately by the application and are specific to each tool.

The number 0 to 9 represents the first 10 handles. You can use the command Cmd.exe to run the program and put the first 10 of the program
Any redirect in the handle. To specify the handle you want to use, type the handle in front of the redirection operator
Digital. If no handle is defined, the default < redirect input operator is 0, and the default > redirect Output
The operator is 1. After you type the > or < operator, you must specify where you want to read or write data. can specify
The file name or another existing handle.

To specify a redirect to an existing handle, use the WITH (&) character, followed by the handle number to be redirected
(for example, & handle #). For example, the following command redirects the handle 2 (that is, STDERR) to the
Handle 1 (i.e. STDOUT):

2>&1

The following table lists the operators that can be used to redirect input and output data streams.

Redirection operator Description
> writes the command output to a file or device, such as a printer, instead of writing it in a command prompt window or handle.
< reads the command input from the file instead of from the keyboard or handle.
>> adds the command output to the end of the file without deleting the information in the file.
>& writes the output of one handle to the input of another handle.
<& reads the input from one handle and writes it to another handle output.
| Reads the output from one command and writes it to the input of another command. Also known as pipelines.

By default, you can send command input (that is, the STDIN handle) from the keyboard to Cmd.exe and then by the
Cmd.exe sends the command output (that is, the STDOUT handle) to the command Prompt window.

redirect Input (<)
To redirect keyboard input to a file or device, use the < operator. For example, to get from File.txt
Enter the sort command, type:

Sort<file.txt

The contents of the File.txt are displayed in a command prompt window as an alphabetical list.

The < operator can open a specified file name with read-only access. Therefore, you cannot use this operator to write to a file
Information. For example, if you start a program with &LT;&AMP;2, all operations that attempt to read handle 0 will fail because the sentence
Handle 2 was originally opened with write-only access.

Attention

0 is the default handle to the < redirect input operator.
Redirected Output (>)
Almost all commands send output to the command prompt window. Even if the output is sent to a drive or printer
The command also displays messages and prompts at the Command Prompt window.

To redirect output from a command prompt window to a file or device, use the > operator. There are many commands that you can
Use this operator. For example, to redirect the dir output to Dirlist.txt, type:

Dir>dirlist.txt

If Dirlist.txt does not exist, Cmd.exe will create the file. If Dirlist.txt exists, Cmd.exe
The output from the dir command is replaced with the information in the file.

To run the Netsh routing dump command, and then send the output to Route.cfg, type:

netsh routing dump>c:\route.cfg

The > operator can open a specified file with write-only access properties. Therefore, you cannot use this operator to read a file.
For example, if you use the redirect >&0 Launcher, all operations that attempt to write to handle 1 will fail because
Handle 0 was originally opened with read-only access.

Attention

1 is the default handle to the > redirect Output operator.
Copy handle
The redirect operator & can copy output or input from one specified handle to another specified handle. For example
To send the Dir output to File.txt and send the error output to File.txt, type:

Dir>c:\file.txt 2>&1

When you copy a handle, you can copy all the attributes of the original state of the handle. For example, if a handle has write-only access to the
property, all copies of the handle have a write-only access property. A handle to a property with read-only access cannot be
Copy to another handle with a write-only access property.

Redirecting inputs and replicas using the & operator
To use the redirect input operator (<) with the copy operator (&), the specified file must already exist.
If the input file exists, Cmd.exe opens the file as read-only and then takes the characters contained in the file as
The input is sent to this command (as in the keyboard input). If a handle is specified, Cmd.exe the specified handle
Copied to the system's existing handle.

For example, to open File.txt in the form of a handle 0 input read (that is, STDIN), type:

<file.txt

To open File.txt and send the output to a command prompt window (that is, STDOUT) after the content is sorted, type:

Sort<file.txt

To find File.txt, then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to
Search.txt, type:

FindFile File.txt>search.txt 2<&1

To copy the user-defined handle 3 as a handle 0 input read (that is, STDIN), type:

<&3

REDIRECT Output and replication using the & operator
If the output is redirected to a file and an existing file name is specified, Cmd.exe will open the file as write-only and
Cover the contents of the file. If a handle is specified, Cmd.exe copies the file to the existing handle.

To copy the user-defined handle 3 to handle 1, type:

>&3

To redirect all output including handle 2 (that is, STDERR) from the ipconfig command to
Handle 1 (that is, STDOUT), and then redirect the output to Output.log, type:

Ipconfig.exe>>output.log 2>&1

Append output using the >> redirect operator
To add output from the command to the end of the file without losing any information that already exists in the file, use two consecutive
The greater than sign (i.e. >>). For example, the following command can append a list of directories generated by the dir command to the
Dirlist.txt file:

Dir>>dirlist.txt

To append the output of the netstat command to the end of Tcpinfo.txt, type:

Netstat>>tcpinfo.txt

Using the pipe operator (|)
The pipe operator (|) can extract the output of one command (STDOUT by default) and then import it into another
The input of a command (by default, STDIN). For example, the following command categorizes the directory:

Dir | Sort

In this example, two commands are started at the same time, but then the sort command pauses until it receives the DIR command
Output. The sort command uses the output of the dir command as input and then sends the output to the
Handle 1 (that is, STDOUT).

Merging commands with the redirect operator
You can create a custom command by merging filter commands with other commands and file names. For example, you can use the
The following command stores the file name containing the string "LOG":

dir/b | Find "LOG" > Loglist.txt

The output of the dir command is sent through the Find filter command. File name containing the string "LOG" as the file name
Lists (for example, NetshConfig.log, LOGDAT.SVD, and Mylog.bat) are stored in the file
The Loglist.txt.

To use multiple filters in the same command, separate the filters with a pipe (|). For example, the following command will
Search each directory on drive C to find the file name that contains the string "LOG", and in the Command Prompt window
Show one screen at a time:

Dir c =/s/b | Find "LOG" | More

The Pipeline (|) can be used to direct the Cmd.exe to send the dir command output through the Find filter command.
The Find command selects only the file name that contains the string "LOG". The more command can be displayed by the Find command selection
File name (one screen at a time in the Command Prompt window). For more information about filter commands, see
Use a filter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.