Skip to main content

How to Redirect Command Output to a File (> and >>) in Batch Script

By default, most command-line programs print their results directly to the console window. For a batch script, however, you often need to capture this output. Saving the output of a command to a file is a fundamental skill, essential for creating log files, generating reports, or saving data for another program to process.

This guide will teach you how to use the standard output redirection operators: the greater-than sign (>) for creating or overwriting files, and the double greater-than sign (>>) for appending to files.

What is Output Redirection?

Output redirection is the process of telling the cmd.exe shell to send the Standard Output of a command to a different destination instead of the console screen. In most cases, this destination is a text file.

  • Standard Output: This is the stream where a command writes its normal, successful results.

The "Overwrite" Operator: >

The single greater-than sign (>) is the "create or overwrite" redirection operator. It is one of the most commonly used operators in all of batch scripting.

Its behavior is simple:

  • If the target file does not exist, it will be created.
  • If the target file already exists, its contents will be completely erased and replaced with the new output.

Syntax: command > filename.txt

The "Append" Operator: >>

The double greater-than sign (>>) is the "append" redirection operator. This is used when you want to add to a file without deleting its existing content.

Its behavior is safe and additive:

  • If the target file does not exist, it will be created (just like >).
  • If the target file already exists, the new output will be added to the very end of the file.

Syntax:command >> filename.txt

Basic Example: Creating a Simple Report

This script uses both operators to generate a simple system information report.

@ECHO OFF
SET "ReportFile=%USERPROFILE%\Desktop\SystemReport.txt"

ECHO --- System Information Report Generator ---
ECHO.

ECHO Creating report header...
REM Use > to create a new file and write the header.
ECHO System Report for %COMPUTERNAME% > "%ReportFile%"
ECHO Generated on %DATE% at %TIME% >> "%ReportFile%"
ECHO. >> "%ReportFile%"

ECHO.
ECHO Appending directory listing for C:\...
REM Use >> to append the DIR command's output to the same file.
DIR C:\ >> "%ReportFile%"

ECHO.
ECHO [SUCCESS] Report has been saved to your Desktop.

This script first creates SystemReport.txt with a header, then appends the directory listing to it.

How Redirection Works

When the cmd.exe interpreter sees a > or >> operator, it performs the redirection before it executes the command.

  1. command > file.txt: The shell first opens file.txt for writing, erasing its contents if it exists. It then connects the command's Standard Output stream directly to this file handle.
  2. command >> file.txt: The shell opens file.txt for writing but seeks to the end of the file. It then connects the command's Standard Output stream to this file handle.

After the connection is made, the command runs. Any text it tries to print to the console is instead sent directly into the file.

Common Pitfalls and How to Solve Them

Problem: The Command's Error Messages Don't Get Redirected

This is the most critical concept to understand. The > and >> operators only redirect Standard Output. They do not redirect Standard Error.

Example of script with error:

REM This command produces both a success and an error.
DIR C:\Windows C:\NonExistentFolder > output.log
  • The listing for C:\Windows (Standard Output) will go into output.log.
  • The "File Not Found" message (Standard Error) will still appear on your console screen.

Solution: To redirect both streams, you must use the special 2>&1 syntax. This is covered in detail in a separate guide, but the short version is: command > file.txt 2>&1

Problem: Creating an Empty File

A common need is to create a new, completely empty file.

Solution: You can redirect the output of "nothing" to a file. The NUL device is perfect for this.

REM This creates a new, 0-byte file named empty.txt.
TYPE NUL > empty.txt

Practical Example: Creating a Timestamped Log File

This script demonstrates a very common pattern: creating a log file, adding a starting entry, running a task, and then adding a finishing entry.

@ECHO OFF
SETLOCAL
SET "LOG_FILE=C:\Logs\MyTask.log"

ECHO --- Task Runner with Logging ---

REM --- Start the log with > to overwrite any previous log ---
ECHO %DATE% %TIME% - Task Started. > "%LOG_FILE%"

ECHO.
ECHO Running the main process...
REM (Imagine a long-running command here)
ECHO Simulating work... >> "%LOG_FILE%"
TIMEOUT /T 5 > NUL

ECHO.
ECHO --- Appending completion status to the log ---
ECHO %DATE% %TIME% - Task Finished. >> "%LOG_FILE%"

ECHO [SUCCESS] Task complete. See log for details.
ENDLOCAL

This script ensures a fresh log is created every time it runs (>) and then adds all subsequent entries using the append operator (>>).

Conclusion

Output redirection is one of the most fundamental and powerful features of the command line.

Key takeaways:

  • Use > (single greater-than) to create a new file or overwrite an existing one. This is for the "first write."
  • Use >> (double greater-than) to append to an existing file or create it if it doesn't exist. This is for all subsequent writes.
  • These operators only redirect Standard Output. Error messages are not captured unless you use more advanced redirection (2>&1).
  • Redirecting output is the primary method for creating logs, reports, and data files from your batch scripts.