Redirection

Maintained on

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.

OperatorDescription
>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>&1Merge 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 typeNumberDescription
Standard output1Normal command results
Standard error output2Error 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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir > filelist.txt
C:\users\user>

Running this command saves the dir output to filelist.txt. Nothing is displayed on screen.

Let’s confirm the saved file contents.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type filelist.txt
Volume in drive C has no label.
Volume Serial Number is 1234-5678
Directory of C:\Users\user\Desktop
2024/12/12 10:30 <DIR> .
2024/12/12 10:30 <DIR> ..
2024/12/12 10:30 1,234 sample.txt
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかる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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo Process started >> log.txt
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo Process completed >> log.txt
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type log.txt
Process started
Process completed
C:\users\user>

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del nonexistent_file.txt 2> error.txt
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type error.txt
Could Not Find nonexistent_file.txt
C:\users\user>

The error message is saved in error.txt.

チェック
Use 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).”

Order of Redirection Matters

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

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir C:\Users\user\ > output.txt 2>&1

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:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>process.cmd > log.txt 2>&1
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type log.txt
Starting process…
Processing file
The system cannot find the file specified.
0 file(s) copied.
Process complete
C:\users\user>

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

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo test > nul

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort < names.txt
Alice
Bob
Charlie
C:\users\user>

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

Quiz 1

Which command correctly saves the output of dir to filelist.txt by overwriting?

回答がサーバーに送信されることはありません
Quiz 2

Which operator redirects standard error output to a file?

回答がサーバーに送信されることはありません
Quiz 3

Which command correctly saves both standard output and standard error to the same file?

回答がサーバーに送信されることはありません

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 file
  • 2>: Save standard error output to a file
  • 2>&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.

In preparation
The linked page is currently under construction. Please wait a moment.
#Command Prompt #Batch File #Redirect #Log #Command