ZetCode

Windows Command Prompt I/O Redirection

last modified July 14, 2025

I/O redirection in Command Prompt allows controlling input and output streams. It enables sending command output to files or other commands. Input can be read from files instead of keyboard. This powerful feature helps automate tasks and process data efficiently.

Cmd handles three standard streams: stdin (input), stdout (output), and stderr (error). These can be redirected using special operators. Streams can be combined, filtered, or saved for later analysis. Mastering redirection is key to effective command-line usage.

Redirection operators include >, >>, <, |, and 2>. They modify how commands interact with streams. Some operators overwrite files while others append. Pipes connect commands by sending one's output to another's input.

This tutorial covers all major I/O redirection techniques in cmd. We'll explore file redirection, piping, stream combining, and error handling. Examples demonstrate practical applications from simple to advanced.

Basic Definitions

Understanding these terms is essential for working with I/O redirection:

Standard Input (stdin): The default input stream (keyboard). Redirected using < operator.

Standard Output (stdout): The default output stream (console). Redirected using > or >> operators.

Standard Error (stderr): The error output stream (console). Redirected using 2> operator.

Pipe (|): Connects stdout of one command to stdin of another. Enables command chaining.

Append (>>): Adds output to existing file instead of overwriting. Preserves previous content.

Basic Output Redirection

The simplest redirection sends command output to a file instead of screen. This creates logs or saves results for later.

redirect.bat
@echo off
echo Redirecting output to file...
dir > filelist.txt
echo Done. Check filelist.txt

This script demonstrates basic output redirection. The dir command's output goes to filelist.txt instead of console.

dir > filelist.txt

Redirects directory listing output to filelist.txt. The > operator creates the file if missing or overwrites existing.

echo Done. Check filelist.txt

Shows a message confirming redirection worked. The echo output still appears on console since not redirected.

C:\>redirect.bat
Redirecting output to file...
Done. Check filelist.txt

Running the script creates filelist.txt containing the directory listing. The console shows only the echo messages.

Appending Output

Append redirection adds to existing files without losing previous content. This is useful for logs accumulating over time.

append.bat
@echo off
echo First line > log.txt
echo Second line >> log.txt
echo Third line >> log.txt
type log.txt

This script shows how to build a file incrementally. Each echo appends to log.txt without erasing prior lines.

echo First line > log.txt

Creates log.txt with initial content using >. Subsequent commands use >> to append rather than overwrite.

echo Second line >> log.txt

Adds second line to log.txt. The >> operator creates file if needed but preserves content if exists.

type log.txt

Displays final file contents to verify all lines were added correctly. The type command shows file content.

C:\>append.bat
First line
Second line
Third line

Output shows the accumulated content of log.txt. Each echo added its line to the growing file.

Error Redirection

Error messages normally appear on screen mixed with regular output. Redirecting stderr separates errors for better handling.

error.bat
@echo off
dir nonexistent 2> errors.txt
dir existent > output.txt 2>&1
echo Error messages:
type errors.txt
echo Output:
type output.txt

This script demonstrates error stream handling. The first command generates an error, the second shows combined streams.

dir nonexistent 2> errors.txt

Attempts to list nonexistent directory, redirecting error to errors.txt. The 2> operator captures only stderr.

dir existent > output.txt 2>&1

Lists existing directory, combining stdout and stderr into output.txt. The 2>&1 redirects stderr to stdout.

type errors.txt

Displays captured error messages. This shows how to isolate and examine errors separately from normal output.

C:\>error.bat
Error messages:
File Not Found
Output:
 Volume in drive C is OS
 Directory of C:\existent
...

Output shows separated error and combined streams. Actual content depends on directory existence.

Input Redirection

Commands can read input from files instead of keyboard. This automates interactive programs or processes prepared input.

input.bat
@echo off
echo John > names.txt
echo Jane >> names.txt
echo Bob >> names.txt
sort < names.txt

This script demonstrates input redirection. It creates a file with names then sorts them using file input.

echo John > names.txt

Creates names.txt with first name. Subsequent appends build a list of names to process.

sort < names.txt

Sorts lines from names.txt alphabetically. The < operator provides file contents as command input.

C:\>input.bat
Bob
Jane
John

Output shows the sorted names from the input file. The sort command never waits for keyboard input.

Piping Commands

Pipes connect commands by sending one's output to another's input. This enables powerful multi-step processing.

piping.bat
@echo off
dir | find "txt" > textfiles.txt
echo Text files found:
type textfiles.txt

This script demonstrates command piping. It lists directory contents, filters for text files, and saves results.

dir | find "txt"

Pipes directory listing to find command which filters for lines containing "txt". The | connects the commands.

> textfiles.txt

Redirects filtered output to textfiles.txt. This combines piping with file redirection for final storage.

C:\>piping.bat
Text files found:
05/01/2025  12:00 PM               120 notes.txt
05/01/2025  12:00 PM               240 report.txt

Output shows only .txt files from directory listing. The exact files depend on your directory contents.

Source

Windows Command Redirection Reference

This tutorial covered essential I/O redirection techniques in Command Prompt. Mastering these concepts enables powerful command-line automation and data processing.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.