Batch File Programming Redirection

Using the CMD Redirection Operators & Pipes with Win32 Command Line

© Guy Lecky-Thompson

Jun 23, 2008
A guide to the various redirection operators (> < and |) used to combine commands from the command line. These are also useful in batch file programming tasks.

Redirecting input and output streams is very useful in batch file programming. It allows the programmer to send output to, or receive input from, locations other than the standard, system defined, input and output locations.

There are several kinds of input and output referred to in this article, each with an identifier that can be used to refer to them on the command line:

  • STDIN : the keyboard, 0
  • STDOUT : the screen, 1
  • STDERR : error output, 2

In addition, there are several operators which can be used to redirect the data in the system:

  • > output redirection
  • >> append output redirection
  • < input redirection
  • | pipe redirection

It is also possible to use the & sign to duplicate the input or output stream from one handle to another, which is slightly different from using the pipe symbol from the above list. The remainder of the article will explain the various redirection operators.

The Output Redirection Operator >

This is used to send output from a command to a file, or other location, as specified by the various locations specified above. If the file does not exist, it will be created. If it does exist, it will be truncated before any data is written, without warning. Be very aware that there is virtually no checking of file names, so if the programmer mistakenly specifies the name of an executable (for example) as output location, the system could become corrupted.

To put a directory listing in a text file, for example, the following could be used:

dir > dir-list.txt

Since the file is opened with write only access, it is not possible to read the contents back in until the command has completed.

The Append Redirection Operator >>

Like the output redirection operator, the >> operator opens the file with write only access, but it does not truncate it. So, any data that is redirected to the file is appended to the end. This is useful for rolling error files, for example, or files that are being created during a longer batch file process.

So, to periodically ping a known location to check network status, it might be useful to redirect the ping command output to a status file:

ping -a ww.google.com >> ping-status.txt

Of course, at some point the file will need to be emptied, or it will continue to grow.

The Input Redirection Operator <

Redirecting input is useful because it allows the programmer to build up a file with output redirection, and then use that output in conjunction with another command, as input. For example, a file can be sorted, line by line, using the sort command:

sort < testfile.txt

This can be used, for example, to sort a directory listing:

C:\>dir /b filelist.txt
C:\>sort filelist.txt

Of course, output will be to the screen, and so the > operator should be sued to redirect the output to a sorted file.

The Pipe Operator |

Finally, the | operator can be used to send the output of one command to the input of another. So, to create a sorted list of files, without passing through an intermediate text file, the following is used:

dir /b | sort

Again, output can be redirected to create a sorted file list:

dir /b | sort > file-list-sorted.txt

With these simple operators, it is possible to create sophisticated solutions without resorting to programming using a traditional language.


The copyright of the article Batch File Programming Redirection in Command Line Programming is owned by Guy Lecky-Thompson. Permission to republish Batch File Programming Redirection in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo