Redirection
When running commands in Command Prompt, there are times when you want to save the output to a file rather than display it on screen.
For example, creating a log file or reviewing the results of a command later.
Redirection is the feature that makes this possible.
This article explains how to use redirection in Command Prompt in detail.
What Is Redirection?
Redirection is a feature that changes where a command sends its output.
Normally, command output is displayed on the screen (the Command Prompt window). Using redirection, you can save that output to a file instead.
Command Prompt supports the following redirection operators.
| Operator | Description |
|---|---|
> | Overwrite a file with standard output |
>> | Append standard output to a file |
2> | Overwrite a file with standard error output |
2>> | Append standard error output to a file |
2>&1 | Merge standard error output into standard output |
< | Read input from a file |
Standard Output and Standard Error Output
To understand redirection, you first need to know about standard output and standard error output.
In Command Prompt, output falls into two categories.
| Output type | Number | Description |
|---|---|---|
| Standard output | 1 | Normal command results |
| Standard error output | 2 | Error messages and warnings |
For example, when dir lists files, the results go to standard output. When you try to delete a file that does not exist, the error message goes to standard error output.
Saving Standard Output to a File (>)
The > operator saves a command’s standard output to a file.
command > filename
Example
Save the output of dir to a file.
Running this command saves the dir output to filelist.txt. Nothing is displayed on screen.
Let’s confirm the saved file contents.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Using the > operator overwrites the target file if it already exists. Existing content will be lost.
Appending Standard Output to a File (>>)
The >> operator appends content to an existing file.
command >> filename
Example
Repeatedly appending to a log file.
Using >> adds new content to the end of the existing file.
Saving Standard Error Output to a File (2>)
The 2> operator saves error messages to a file.
command 2> filename
Example
Save the error from trying to delete a file that does not exist.
The error message is saved in error.txt.
2>> to append error messages to an existing file. Saving Both Output and Errors to the Same File (2>&1)
To save both normal output and error messages to the same file, use 2>&1.
command > filename 2>&1
2>&1 means “merge standard error output (2) into standard output (1).”
command > file.txt 2>&1 saves both stdout and stderr to file.txt, but
command 2>&1 > file.txt does not produce the same result.
When saving both, always specify the stdout destination first, then write 2>&1.
Example
This saves both the dir output and any error messages to output.txt.
Example in a Batch File
@echo off
setlocal
echo Starting process...
rem Normal processing (stdout)
echo Processing file
rem Processing that may produce an error
copy nonexistent.txt backup.txt
echo Process complete
endlocal
Running this batch file and saving all output to a log file:
Suppressing Output (nul)
To completely suppress a command’s output, redirect to nul.
nul is a special file — anything written to it is discarded.
command > nul
Example
Running this command displays nothing on screen.
Suppressing Both Stdout and Stderr
command > nul 2>&1
This pattern is commonly used in batch files.
@echo off
setlocal
rem Completely suppress command output
copy sample.txt backup.txt > nul 2>&1
if errorlevel 1 (
echo Copy failed
) else (
echo Copy completed
)
endlocal
Input Redirection (<)
The < operator passes file contents as input to a command.
command < filename
Example
Sort file contents using the sort command.
Practical Sample Code
Creating a Log File
@echo off
setlocal
set LOGFILE=process_log.txt
rem Initialize log file with timestamp
echo ========================================= > %LOGFILE%
echo Start: %DATE% %TIME% >> %LOGFILE%
echo ========================================= >> %LOGFILE%
rem Log process details
echo Current directory: %CD% >> %LOGFILE%
echo. >> %LOGFILE%
echo File list: >> %LOGFILE%
dir /b >> %LOGFILE% 2>&1
rem End
echo. >> %LOGFILE%
echo End: %DATE% %TIME% >> %LOGFILE%
echo Log file created: %LOGFILE%
endlocal
Separating Error Logs
@echo off
setlocal
rem Save stdout and stderr to separate files
mycommand.exe > success.log 2> error.log
rem Check whether any errors occurred
if exist error.log (
for %%A in (error.log) do (
if %%~zA GTR 0 (
echo Errors occurred. Check error.log.
) else (
echo Completed successfully.
)
)
)
endlocal
Practice Quiz
Summary
Redirection is a very useful feature when working in Command Prompt.
Key points:
>: Overwrite a file with standard output>>: Append standard output to a file2>: Save standard error output to a file2>&1: Merge standard error into standard output> nul: Suppress all output<: Read input from a file
Using these features, you can create log files, handle errors, and automate batch file operations much more efficiently.