Redirecting Stream Handles in Pure Batch

As you know there are these redirection operators:

  • > Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.
  • < Reads the command input from a file, instead of reading input from the keyboard.
  • >> Appends the command output to the end of a file without deleting the information that is already in the file.
  • | Reads the output from one command and writes it to the input of another command. Also known as a pipe.

But there are also these operators:

  • >& Writes the output from one handle to the input of another handle.
  • <& Reads the input from one handle and writes it to the output of another handle.

By default, you send the command input (that is, the STDIN handle) from your keyboard to Cmd.exe, and then Cmd.exe sends the command output (that is, the STDOUT handle) to the Command Prompt window. But why follow the rules?

Take a look at the numeric values for the handles in cmd:

Handle: STDIN
Value: 0
Description: Keyboard Input

Handle: STDOUT
Value: 1
Description: Console Output

Handle: STDERR
Value: 2
Description: Console Error Output

And the rest are handles 3-9 that are empty and/or
defined by individually by other applications.

Now to mess with the handles, you want to specify the handle
numbers with the handle redirection operators on the same line
as your command followed by a space after your command.

This command will redirect the command output and including STDERR to STDOUT, then STDOUT is redirected to output.log

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

My Source:

One thought on “Redirecting Stream Handles in Pure Batch

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: