Redirection

   command > filename       Redirect command output to a file

   command >> filename       APPEND into a file

   command < filename        Type a text file and pass the text to command

   commandA  |  commandB     Pipe the output from commandA into commandB

   commandA &  commandB      Run commandA and then run commandB
   commandA && commandB      Run commandA, if it succeeds then run commandB
   commandA || commandB      Run commandA, if it fails then run commandB

Numeric handles:

STDIN  = 0  Keyboard input
STDOUT = 1  Text output
STDERR = 2  Error text output
UNDEFINED = 3-9

   command 2> filename       Redirect any error message into a file
   command 2>> filename      Append any error message into a file
  (command)2> filename       Redirect any CMD.exe error into a file
   command > file 2>&1       Redirect errors and output to one file
   command > file 2<&1       Redirect output and errors to one file
   command > fileA 2> fileB  Redirect output and errors to separate files

   command 2>&1 >filename    This will fail!

Redirect to NUL (hide errors)

   command 2> nul            Redirect error messages to NUL
   command >nul 2>&1         Redirect error and output to NUL
   command >filename 2> nul  Redirect output to file but suppress error
  (command)>filename 2> nul  Redirect output to file but suppress CMD.exe errors

Note, any long filenames must be surrounded in "double quotes". A CMD error is an error raised by the command processor itself rather than the program/command.

Redirection with > or 2> will overwrite any existing file.

You can also redirect to a printer with > PRN or >LPT1

To prevent the > and < characters from causing redirection, escape with a caret: ^> or ^<

Pipes and CMD.exe

When a command is piped with '| batch_command ' this will start (or rather CALL) a new CMD.exe instance, in effect running:

C:\Windows\system32\cmd.exe /C /S /D "batch_command"

This has several side effects:
Any newline (CR/LF) characters in the batch_command will be turned into & operators. (an in depth discussion of this on StackOverflow)
If the batch_command includes any caret escape characters ^ they will need to be doubled up so that the escape survives into the new CMD shell.
Calling a new CMD shell also has a small effect on performance, this may not be noticable for small data sets.

Examples of redirection:

   DIR >MyFileListing.txt
   
   DIR /o:n >"Another list of Files.txt"

   ECHO y| DEL *.txt

   ECHO Some text ^<html tag^> more text
   
   MEM /C >>MemLog.txt

   Date /T >>MemLog.txt

   SORT < MyTextFile.txt

   SET _output=%_missing% 2>nul

   DIR C:\ >List_of_C.txt 2>errorlog.txt
   
   FIND /i "Jones" < names.txt >logfile.txt

   DIR C:\ >List_of_C.txt & DIR D:\ >List_of_D.txt

   ECHO DIR C:\ ^> c:\logfile.txt >NewScript.cmd

   (TYPE logfile.txt >> newfile.txt) 2>nul

“Stupidity, outrage, vanity, cruelty, iniquity, bad faith, falsehood / we fail to see the whole array when it is facing in the same direction as we” ~ Jean Rostand (French Historian)

Related:

Syntax
Q245031 - Error when using the | pipe symbol
Equivalent bash command (Linux): Redirection - Spooling output to a file, piping input



Back to the Top

© Copyright SS64.com 1999-2012
Some rights reserved