\
Skip Navigation LinksHome > Coding > Scripting > MS-DOS > View Item Info\
 
 
[]Return to List
[]UDF / UDC Library
[]Script Library



 

Showing: > (Redirection) Commands

Redirection is an operator, not a command. Redirection tells COMMAND.COM to connect the input or output of programs to either files (redirection) or other programs (pipes). This works only if the program uses the standard input and output streams for its console I/O. In general, programs with fancy screens don't and those with crude command line interfaces do. Most DOS utilities do. Nearly all filters do.

It is useful to know that when reading a line that uses pipes and redirectors, the line is read right to left if pipes are used prior to the redirection, then left to right after the redirection symbol. Graphical example:
echo. | date | find "Current" >> foo.log
<----r---e---a---d---------- then -r-e-a-d-->

English read: "Find/output the line starting with the word ‘current’ when running the Date program, using the generated by the echo command to answer the programs question, and display the find results as an addition to a file called foo.log."

Output redirection is more generally used - it is useful for putting directory listings into files and for creating and filling files from inside batch programs, using the ECHO command and redirection to change the destination of the ECHO to a file instead of the console. > causes the file to be created or truncated - multiple ECHOs to the same file will leave the file with just the last one in it while >> causes whatever is redirected to be appended to the end of the file. >> is useful for building batch files from within batch files: the file is created with a > redirection and the remaining ECHO lines use >> to add the lines to the end of the file.

Note that output redirection and pipes work only with the standard output stream (STDOUT) and the standard input stream (STDIN).

There are many gotchas and subtleties to redirection and pipes: in complex commands only the output of the first command is redirected, and that is often something like if exist or FOR that doesn't even have an output in the usual sense.
*

Command > FileName
[Arguments] |  [Constants] |  [Return Type] |  [Script Example]

Arguments
Command
Any valid command or application which writes output to the console.

FileName
Path and name of a file to create and write the output of the command to. If this file does not exist, it will be created. If it does exist, it will be overwritten.


Constants
MS-DOS does not use any constants


Return Type
None.


Script Example
Unless otherwise noted, this code is licensed according to the terms and conditions listed here.
@ECHO OFF

:: The Format command will ask you to press enter to start the format
:: This will write a return character to a new file which will be used
:: as the first input to the command
ECHO > C:\DEMO.TXT
:: The next input asked by the format command is a disk label
:: This appends the file and will serve as the second input
ECHO NewDisk >> C:\DEMO.TXT
:: The last request is to format another
:: This appends the file with the No response
ECHO N >> C:\DEMO.TXT
ECHO >> C:\DEMO.TXT

::Now we format a disk
FORMAT A: < C:\DEMO.TXT

:: And Clean up after ourselves
DEL C:\DEMO.TXT

* This text may contain reprinted material from the Microsoft .

Help Support Dx21
Buy Thru Us

 FAQs  |  Terms Of Use  |  Privacy Policy  |  Contact Us
Copyright © 1997 - 2010 Dx21, LLC. All rights reserved.
Dx21, LLC a Washington Limited Liability Company
Page Rendered at: 9/8/2010 10:21:18 PM for Unknown