What are Redirection Operators in Dos with example and how-to?

Card Puncher Data Processing

About

This page talks about shell redirection operator|operator in DOS.

The order of redirections is significant when redirecting a standard error stream to a standard output stream. See order of redirections

Handle

The standard predefined stream that can handle the operators are the below one and have a unique id

Handle Numeric equivalent of handle (Unique id) Description
nul or another string nul Suppress the character flow
STDIN 0 Keyboard input
STDOUT 1 Output to the Command Prompt window
STDERR 2 Error output to the Command Prompt window
UNDEFINED 3-9 These handles are defined individually by the application and are specific to each tool.

Operators

Redirecting Output

> (Greater than)

Greater than > redirect:

  • a standard output stream (stdout, stderr, …)

to:

  • a standard output stream (generally stderr to stdout)
  • or to new file

Syntax:

[SourceHandleId]>[&]TargetWord

where:

  • SourceHandleId is the unique id of an handle by default 1 (stdout). One is the default handle (ie > is equivalent to >1 (stdout))
  • & indicates that the targetWord is a file descriptor and not a filename
  • TargetWord is a word that is subject to expansion (ie you can use a variable, …) but normally you will set it to
    • TargetHandleId is the unique id of an handle if & is present
    • or filePath a file path that must be created or overwritten.

Example:

  • Redirect stderr to a new file
command 2> stderr.log
  • Redirect stdout to a new file
command 1> stdout.log
Rem or simply
command > stdout.log
  • Redirect stderr to a new file as variable
set FILE_STDERR=err.log
command 2> %FILE_STDERR%
  • Redirecting the stderr to nothing (discarding it)
command 2>nul
  • Redirecting standard output and error output to File.txt. The redirections of the stderr to stdout must appears after, the redirection to the file. See order_of_precedence
dir >c:\file.txt 2>&1

» (Append)

>> has the same syntax than > but it will append to the file. It will not create it or truncate as > does.

Redirecting Input - < (Less than)

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

[StandardStreamHandleId]<[&]SourceWord

where:

  • HandleId is a handle of a standard stream (generally 2)
  • & indicates that the targetWord is a file descriptor and not a filename
  • SourceWord is a word that is subject to expansion (ie you can use a variable, …) but normally you will set it to
    • SourceHandleId is the unique id of an handle if & is present. Zero is the default handle (ie < is equivalent to <0 (stdin))
    • or filePath a file path that must be created or overwritten.

Example:

  • Sort the command input from a file (File.txt) The < operator opens the specified file name with read-only access.
sort  <file.txt
REM or 
<file.txt  sort  
  • Redirecting handle 2 (STDERR) into handle 1 (STDOUT):
2<&1

Redirecting Output to Input - | (Pipeline or Command Chaining)

The | pipeline (See also DOS - Filter Commands) operator redirect the stdout (not the stderr) of the source program to the stdin of the target program

Syntax:

command parameter | command parameter

If you want to pipe :

  • the stderr with the stdout, you need to redirect stderr to stdout.
mySourceCommand  2>&1 | myTargetCommand
  • only stderr without stdout, you need to discard the stdout and redirect
mySourceCommand  2>&1 1>null | myTargetCommand

Order of precedence

The order of the standard streams redirection is important

The order of redirections is significant when you redirect the standard streams. The redirections of standard streams happens from left to right (and not from right to left)

dir >targetFile.txt 2>&1

redirects to the file:

  • both standard output (file descriptor 1)
  • and standard error (file descriptor 2)
dir 2>&1 >targetFile.txt

redirects only the standard output to the file.

the standard error was made a copy of the standard output before the standard output was redirected to the file.

The redirections may appears before or after the command

  • Example 1
command 2>&1 >targetFile.txt
Rem is equivalent to
2>&1 >targetFile.txt command
  • Example 2: A command can have both its standard input and standard output redirected.
sort < file_to_be_sorted.txt > sorted_file_list.txt
Rem is equivalent to
< fileToBeSorted.txt > fileSorted.txt sort

How to

Create a file with content

DOS - File

set myFile=myFile.sql
> %myFile% echo.WHENEVER SQLERROR EXIT SQL.SQLCODE
>> %myFile% echo.select * from dual;
>> %myFile% echo.exit;
>> %myFile% echo.

Content:

WHENEVER SQLERROR EXIT SQL.SQLCODE
select * from dual;
exit;

Documentation / Reference





Discover More
Card Puncher Data Processing
DOS - (Control Operators | Command Separator)

Dos supports the following control operator in order to control or separate command instruction. Operator Description & all commands run serially without error checking && execute the next command...
Card Puncher Data Processing
DOS - File

File in DOS. See ... in a for loop: the file name without extension. Fully Qualified Path Name See if exist statement See: ERASE Deletes one or more files. type...
Card Puncher Data Processing
DOS - Filter Commands

in Dos. With the command redirection pipe character (|), a command filter is a command within a command that: reads the command's input (input stream), transforms the input, and then writes the...
Card Puncher Data Processing
DOS - Special Characters

This article tries to list all special characters and their meanings. Character Description Articles | pipeline > stdout < stdin 2> stderr >> append & duplicate handle or control...
Card Puncher Data Processing
Dos - (Batch) Script

The batch script is a text file with the extension .bat or .cmd that is interpreted by a batch interpreter. You can call a batch script by : writing its name in the source script: The script execution...
Card Puncher Data Processing
Dos - Command

A command is: a Dos command (See below). of an utility. The DOS command are extended with management tools located in C:\Windows\System32...
Card Puncher Data Processing
Dos - File System (file, directory)

see The temp variable specifies the directories where temporary files should be placed The cd variable will give the current directory
Card Puncher Data Processing
Dos - Pipeline

Pipelines (or Pipe) is a redirection operator that are used to chain the output of a command to the input of an other.. See
Card Puncher Data Processing
Dos - Type

Displays the contents of a text file or files. By using it with the redirection operators, you can concatenate files. See type is the equivalent of the cat command for Linux. Example
Command Line Windows Process Explorer
Process - Command (Command line)

A OS command is a line of text that will create a process instance from a program (also known as image) . A command line is just a one line of text: with a path to a executable file and arguments...



Share this page:
Follow us:
Task Runner