How to Redirect Output in Batch Files

Redirection Summary

command > fileWrite standard output of command to file
command 1> fileWrite standard output of command to file (same as previous)
command 2> fileWrite standard error of command to file
command > file 2>&1Write both standard output and standard error of command to file
command >> fileAppend standard output of command to file
command 1>> fileAppend standard output of command to file (same as previous)
command 2>> fileAppend standard error of command to file
command >> file 2>&1Append both standard output and standard error of command to file
commandA | commandBRedirect standard output of commandA to standard input of commandB
commandA 2>&1 | commandBRedirect standard output and standard error of commandA to standard input of commandB
command < file command gets standard input from file
command 2>&1command's standard error is redirected to standard output
command 1>&2command's standard output is redirected to standard error

Using NUL

ECHO Hello world>NULRedirect Standard Output stream to the NUL device
EHCO Hello world 2>NUL Redirect Standard Error stream to the NUL device. Note: "EHCO" spelling error is intentional.
ECHO Hello world > NUL 2>&1 Redirected Standard Output and Standard Error to the NUL device
ECHO Hello world > hello.txt 2>&1 Redirect Standard Output and Standard Error to a file
test.bat > testlog.txt 2> testerrors.txt Redirect Standard Output to testlog.txt and Standard Error to testerros.txt.
ECHO Hello world>CON Redirect Output to screen no matter what. Even if the script's output is redirected.

An Example:

Creat the batch file on the left below, execute it form the command line and redirect the ouput:

test.batoutput
  @ECHO OFF
  ECHO This text goes to Standard Output
  ECHO This text goes to Standard Error 1>&2
  ECHO This text goes to the Console>CON
  C:\>test.bat
  This text goes to Standard Output
  This text goes to Standard Error
  This text goes to the Console

  C:\>test.bat > NUL
  This text goes to Standard Error
  This text goes to the Console

  C:\>test.bat 2> NUL
  This text goes to Standard Output
  This text goes to the Console

  C:\>test.bat > NUL 2>&1
  This text goes to the Console
See also: