<< Click to Display Table of Contents >>

Navigation:  TCC-RT > Input / Output Redirection > Redirection and Pipes >


Redirection can be used to reassign the standard input (stdin), standard output (stdout), and standard error (stderr) devices from their default settings (the keyboard and screen) to another device such as NUL or serial port, to a file, or to the Windows clipboard. You must use some discretion when you use redirection with a device.


Redirection always applies to a specific command, and lasts only for the duration of that command. When the command is finished, the assignments for standard input, standard output, and standard error revert to whatever they were before the command.


TCC's output is normally in ANSI. If you want to redirect output in Unicode, you need to either use the /U startup option in TCC, or the Unicode Output option in TCMD.INI.


In the descriptions below, filename means either the name of a file or of an appropriate device (CON for the keyboard and screen; CLIP: for the clipboard; NUL for the "null" device, etc.).


Here are the standard redirection options supported by TCC (see below for additional redirection options using numeric file handles):


Onestep        Input redirection

Onestep        Output redirection

Onestep        Special considerations for specific commands

Onestep        NoClobber

Onestep        Multiple redirections

Onestep        Creating an empty file

Onestep        Redirection by handle

Onestep        "Here-document" redirection

Onestep        "Here-string" redirection


Input redirection


< filenameTo get input from a file or device instead of from the keyboard.


Output redirection




standard output

>   filename

>>   filename

standard error

>&> filename

>>&> filename

merge standard output and standard error

>&  filename

>>&  filename


To use redirection, place the redirection symbol and filename at the end of the command line, after the command name and any parameters. For example, to redirect the output of the DIR command to a file called DIRLIST, you could use a command line like this:


dir /b *.dat > dirlist


You can use any combination of input and output redirection for the same command, as appropriate for your purpose. For example, this command sends input to the external program SORT from the file DIRLIST, and sends output from SORT to the file DIRLIST.SRT:


sort < dirlist >


You can redirect text to or from the Windows clipboard by using the pseudo-device name CLIP: (the colon is required).


If you redirect the output of a single internal command like DIR, the redirection ends automatically when that command is done. If you start a batch file with redirection, all of the batch file's output is redirected, and redirection ends when the batch file is done. Similarly, if you use redirection after the closing parenthesis of a command group (e.g.,  ...) > report), all of the output from the command group is redirected, and redirection ends when the command group is done.


Special considerations for specific commands


You cannot redirect all output from the execution of a DO loop due to the restriction that the DO command and its matching ENDDO may not be part of a command group.


To redirect the output of a TEXT command, append the redirection syntax to the TEXT command.


When you execute a FOR or GLOBAL command, redirection is separately performed for each iteration, based on the directory current for that iteration. This can result in repeated overwriting of the output file, or the creation of a separate output file in each directory. To generate a single, cumulative output file, use Command Grouping as in the example below:


 ( for /r %f in (*.btm) echo %@full[%f] ) > c:\temp\btmlst




When output is directed to a file with >, >&, or >&>, and that file already exists, it will be overwritten. You can protect existing files by using the SETDOS /N1 command.


When output is appended to a file with >>, >>&, or >>&>, the file will be created if it doesn't already exist. However, if the NoClobber mode is set as described above, append redirection will not create a new file; instead, if the output file does not exist a "File not found" or similar error will be displayed.


You can temporarily override the current setting of NoClobber by using an exclamation mark [!] after the redirection symbol. For example, to redirect the output of DIR to the file DIROUT, and allow overwriting of any existing file despite the NoClobber setting:


dir >! dirout


Multiple redirections


Redirection is fully nestable. For example, you can invoke a batch file and redirect all of its output to a file or device. Output redirection on a command within the batch file will take effect for that command only; when the command is completed, output will revert to the redirected output file or device in use for the batch file as a whole.


Creating an empty file


You can use redirection to create an empty (zero-byte) file. To do so, enter >filename as a command, with no actual command before the > character.


Redirection by handle


In addition to the redirection options above, TCC also supports the CMD syntax:


n>fileRedirect handle n to the named file
n>&mRedirect handle n to the same place as handle m


Warning: You may not put any spaces between the n and the >, or between the >, &, and m in the second form. The values of n and m must be single decimal digits, and represent file handles. Windows defines 0, 1, and 2 as shown in the table below.





standard input


standard output


standard error


The n>file syntax redirects output from handle n to file. You can use this form to redirect two handles to different places. For example:


dir > outfile 2> errfile


sends normal output to a file called OUTFILE and any error messages to a file called ERRFILE.


The n>&m syntax redirects handle n to the same destination as the previously assigned handle m. For example, to send standard error to the same file as standard output, you could use this command:


dir > outfile 2>&1


Notice that you can perform the same operations by using standard redirection features. The two examples above could be written as


dir > outfile >&> errfile




dir >& outfile


"Here-document" redirection


Wherever input redirection is supported, you can use a Linux-like "here-document" approach. The syntax is:


program << word


The current batch file is read up to the next occurrence of word, and the resulting text becomes standard input to program. For example:


c:\test\program.exe << endinput

input 1

input 2

input 3


echo This is the next line after "program.exe"


Special features of "here document":


If the << is followed by a hyphen (-), the leading white space on the following lines will be removed before passing them to program (i.e. they will be effectively left-justified).


The parser will perform variable expansion on each line, unless the word following << is enclosed in double quotes.


"Here-string" redirection


The "here-string" lets you send string text directly to a program's input. The syntax is:


program <<< string


This is similar to using KEYSTACK, but easier to enter for text input. (If you need to send special keys or insert waits, you'll need to use KEYSTACK.)  For example, to send a string to the standard input of program:


c:\test\program.exe <<< This is some input text.