Programming Field

‘>’ (Output redirection) - DOS/Command Prompt Reference

Writes output of the command, specified on the left side of the symbol, to the file, specified on the right side. (Overwrites if the file exists.)


<command> [<num>]> <file-name>
<command> [<num>]>&<num>
<command> Specifies the command line to send output to the file.
<file-name> Specifies the file name for output.
[<num>] [Windows NT series] Specifies the handle number of redirection source. The handle number can be ‘1’ (standard output (STDOUT)) or ‘2’ (standard error (STDERR)), and those output will be redirected. If the handle number is not specified, ‘1’ (STDOUT) will be used.
&<num> [Windows NT series] Specifies the handle number of redirection target. The handle number can be ‘1’ (standard output (STDOUT)) or ‘2’ (standard error (STDERR)), and output will be written to those target. For example, Echo command usually prints strings to standard output, but if you specify ‘>&2’ at the end of the command line, you can print strings to standard error.


Usage of ‘>’ 1 Usage of ‘>’ 2

By using output redirection, you can write and save output, usually printed to the screen, to the file. Output redirection has a role to change standard output target to specified file (instead of console).


Sample 1

foo.exe > out.txt

Saves output of ‘foo.exe’ program (written to STDOUT) to the file ‘out.txt’.

Sample 2

type CON > save.txt

Executes ‘outputs CON (console input)’ with Type command, which means ‘outputs keyboard input’, and saves output content (i.e. keyboard input) to the file ‘save.txt’.
(This command line allows you to create text file immediately. When finishing input, pressing Ctrl+Z to output ‘^Z’ and entering newline will exit input mode. Please see also samples of Copy command.)

Sample 3

hoge.exe 1> out.txt 2> err.txt

[Windows NT series] Of the output by ‘hoge.exe’, writes standard output to ‘out.txt’ and error output to ‘err.txt’. This allows for some separation of the information you need.

Sample 4

echo Error.>&2

[Windows NT series] Changes output target of Echo to standard error instead of standard output. This is especially useful in batch file; for example given the batch file ‘aaa.bat’ includes this command line, even if ‘aaa.bat > NUL’ is executed on Command Prompt, ‘Error.’ is printed to the screen. (This does not apply if standard error is redirected by another way.)