Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:docs:cmd:other:redirection [2014/05/24 05:28] – valerius | en:docs:cmd:other:redirection [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Redirection ===== | ||
- | |||
- | |||
- | Redirection can be used to reassign the **standard input**, **standard output**, and **standard error** devices from their default settings (the keyboard and screen) to another device like the printer or serial port, to a file, or to the clipboard. You must use some discretion when you use redirection with a device; there is no way to get input from the printer, for example. | ||
- | |||
- | 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. | ||
- | |||
- | In the descriptions below, **filename** means either the name of a file or of an appropriate device (**PRN**, **LPT1**, **LPT2**, or **LPT3** for printers; **COM1** to **COM4** for serial ports; **CON** for the keyboard and screen; **CLIP**: for the clipboard; **NUL** for the " | ||
- | |||
- | Here are the standard redirection options supported by **CMD.EXE** (see below for additional redirection options using numeric file handles): | ||
- | |||
- | < | ||
- | **< filename** To get input from a file or device instead of from the keyboard | ||
- | **> filename** Redirect standard output to a file or device | ||
- | **>& filename** Redirect standard output and standard error to a file or device | ||
- | **>&> | ||
- | </ | ||
- | |||
- | If you want to append output to the end of an existing file, rather than creating a new file, replace the first ">" | ||
- | |||
- | To use redirection, | ||
- | |||
- | < | ||
- | [c:\] dir /b *.dat > dirlist | ||
- | </ | ||
- | |||
- | You can use both input and output redirection for the same command, if both are appropriate: | ||
- | |||
- | < | ||
- | [c:\] sort < dirlist > dirlist.srt | ||
- | </ | ||
- | |||
- | You can redirect text to or from the **OS/2** 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, | ||
- | |||
- | When output is directed to a file with >, >&, or >&>, | ||
- | |||
- | When output is appended to a file with >>, >>&, | ||
- | |||
- | 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]]**, | ||
- | |||
- | < | ||
- | [c:\] dir >! dirout | ||
- | </ | ||
- | |||
- | 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. | ||
- | |||
- | You can use redirection if you need to create a zero-byte file. To do so, enter **> | ||
- | |||
- | In addition to the standard redirection options above, **CMD.EXE** also supports the **OS/2** **CMD.EXE** syntax: | ||
- | |||
- | |**n> | ||
- | |**n>& | ||
- | |||
- | **[n]** and **[m]** are one-digit file handles between 0 and 9. You may not put any spaces between the n and the >, or between the **>**, **&**, and **m** in the second form. **OS/2** interprets " | ||
- | |||
- | The **n> | ||
- | |||
- | < | ||
- | [c:\] dir > outfile 2> errfil | ||
- | </ | ||
- | |||
- | sends normal output to a file called OUTFILE and any error messages to a file called ERRFILE. | ||
- | |||
- | The **n>& | ||
- | |||
- | < | ||
- | [c:\] .dir > outfile 2>&1 | ||
- | </ | ||
- | |||
- | Notice that you can perform the same operations by using standard **CMD.EXE** redirection features. The two examples above could be written as | ||
- | |||
- | < | ||
- | [c:\] dir > outfile >&> | ||
- | </ | ||
- | |||
- | and | ||
- | |||
- | < | ||
- | [c:\] dir >& | ||
- | </ | ||