Output Streams

Introduction

This page was written because in October 2025, a Reddit user asked about redirecting the the nul device. in a batch file. I thought I would take a look at streams, devices, redirection and pipes as they are used in batch files.


Streams

Programs have three standard streams or communication channels available to them. These are Standard input on stream 0, Standard output on stream 1, and Standard error on stream 2.

Stream 0 is used for input and by default comes from the keybaord. Stream 1 is for output and the defalt for that is the screen. Stream 2 is for error measages and the default for those is also the screen.


Devices

There are reserved names that must not be used as file names. These are CON, PRN, AUX, NUL, COM, COM(0-9), LPT, and LPT(0-9). One reason these are reserved is because they are the names of devices. CON refers to the console, which consists of the screen and keyboard. PRN and LPT are parallel ports, AUX are auxiliary devices, COM are the serial communication ports. NUL is slightly different in that it is a device file that discards all data written to it.

The conventions date from the 1960s, but now so deeply embedded they are still reserved.

The print command still tries to open and use the PRN device but it no longer works works reliably because most printers are now wireless or use USB ports. For example:

print myfile.txt

returns

Unable to initialize device PRN


Redirection

Redirection is the passing of the output of one command to another stream, file or device. In order to do this, the symbols <, >, and >> are used. Piping is the redirection of the output of one program to the input of another and for this the | symbol is used.

Using Rob van der Woude's redirection table:

Command Effect Example
command 2> file Write standard error of command to file dirx 2> outfile.txt
command > file 2>&1 Write both standard output and standard error of command to file dir *.not > outfile.txt 2>&1
command >> file
OR
command 1>> file
Append standard output of command to file dir >> outfile.txt
command 2>> file Append standard error of command to file dir *.not 2>> outfile.txt
command >> file 2>&1 Append both standard output and standard error of command to file dir *.not >> outfile.txt 2>&1
commandA | commandB Redirect standard output of commandA to standard input of commandB dir | find "outfile.txt"
commandA 2>&1 | commandB Redirect standard output and standard error of commandA to standard input of commandB dir *.not 2>&1 | find "outfile.txt"
command < file Command gets standard input from file dir < infile.txt
command 2>&1 Command's standard error is redirected to standard output dir *.not 2>&1
command 1>&2 Command's standard output is redirected to standard error dir *.not 1>&2

Using > and >> to a file will create the file if it does not already exist. The difference being that > will replace any content the file already has while >> will append to the content already there.

Some commands are commonly used with the pipe command, such as more, which pages the output, and find, which finds strings.

Both standard output and standard error can be redirected to the NUL device, effectively losing it so it cannot be seen. For example, the command dir *.not returns:

Volume in drive C is OS
Volume Serial Number is

Directory of C:\Users\

File Not Found

The first lines are standard output, but the last line is standard error.

dir *.not >nul

returns

File Not Found

This is because the command sends standard output to NUL.

dir *.not 2>nul

returns

Volume in drive C is OS
Volume Serial Number is

Directory of C:\Users\

This is because standard error is being sent to NULL.

To hide both standard output and standard output, then use:

dir *.not >null 2>&1

This doesn't return anything because both standard output and standard error streams are being sent to NUL.


Sources and Resources

Display & Redirect Output (Rob van der Woude's Scripting Pages)
File descriptor (Computer Hope)
Naming Files, Paths, and Namespaces (Microsoft Learn)
Null device (Wikipedia)
Peripheral Interchange Program (Wikipedia)
PowerShell about Redirection (Microsoft Learn)
Print Files from Batch Files (Rob van der Woude's Scripting Pages)
Redirection (Rob van der Woude's Scripting Pages)