Skip to main content

Batch Script Redirection Operators

Redirection Operators

Redirection is a concept of taking the output of a command and re-directing that output to a different output media

The following table lists the available redirection operators.

OperatorsDescriptionExample
>Redirect command output to a filecommand > filename
>>Append command output into a filecommand >> filename
<Pass the text to commandcommand < filename
|Redirect first command standard output to second command standard inputcommandA | commandB

The following code snippets show how the various redirection operations can be used.

command > filename

This command redirects command output to a file.

@echo off 
ipconfig>C:\details.txt

The output of the above program would be that all the details of the ipconfig command will be sent to the file C:\details.txt. If you open the above file, you might see the information similar to the one as the following.

Windows IP Configuration
Wireless LAN adapter Local Area Connection* 11:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Tunnel adapter Teredo Tunneling Pseudo-Interface:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :

command >> filename

This command appends the output of the command into a file.

@echo off
systeminfo>>C:\details.txt

The output of the above program would be that all the details of the systeminfo command will be appended to the file C:\details.txt. if you open the above file you might see the information similar to the one as the following.

Windows IP Configuration
Wireless LAN adapter Local Area Connection* 11:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Tunnel adapter Teredo Tunneling Pseudo-Interface:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Host Name: WIN-12AL345675
OS Name: Microsoft Windows Server 2019
OS Version: 10.0.20348
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Server
OS Build Type: Multiprocessor Free
Registered Owner: Windows User
Registered Organization:
Product ID: 00123-40000-00000-AA567
Original Install Date: 03/03/2022, 10:24:34 AM
System Boot Time: 03/30/2022, 10:52:15 AM
System Manufacturer: IBM
System Model: 12345
System Type: x64-based PC

command < filename

This command types a text file and passes the text to command.

@echo off
sort < Example.txt

If you define a file called Example.txt which has the following data.

4
2
3
1

The output of the above program would be

1
2
3
4

commandA | commandB

This command redirects standard output of commandA to standard input of command.

echo y |  del  *.txt

The above command will pass the option of y which is the value of Yes to the command of del. This will cause the deletion of all files with the extension of txt.

Standard Input/Output/Error Redirection

The > and >> operators allow you to redirect standard output (stdout 0), input (stdin 1), error (stderr 2).

See below the examples with the standard error:

command 2> file

This command writes the standard error of command to file (OS/2 and NT).

dir C:\ >List_of_C.txt 2>errorlog.txt

In the above example, if there is any error in processing the command of the directory listing of C, then it will be sent to the log file errorlog.txt.

command 2>> file

Appends the standard error of command to file (OS/2 and NT).

dir C:\ >List_of_C.txt 2>errorlog.txt
dir D:\ >List_of_D.txt 2>>errorlog.txt

In the above example, if there is any error in processing the command of the directory listing of D, then it will be appended to the log file errorlog.txt.