6.3. Wildcards, Pipes, and Redirection
In
addition to the various command-line parameters used by each of the
commands documented in this chapter (and the components documented in
Chapter 4), there are certain symbols used on
the command line that have special meaning. Table 6-1 shows these special symbols and what they do.
They must be used in conjunction with other commands (they
don't stand alone) and can be used in the Command
Prompt window, in Start
Run, and in any Address Bar.
Symbol | Description |
---|---|
* | Multiple-character wildcard, used to specify a group of files. |
? | Single-character wildcard, used to specify multiple files with more precision than *. |
. | One dot represents the current directory; see "cd or chdir". |
.. | Two dots represent the parent directory; see "cd or chdir". |
... (three dots) | Three dots represent the grandparent directory; see "cd or chdir". |
\ | Separates directory names, drive letters, and filenames. By itself, represents the root directory of the current drive. |
> | Redirects a command's text output into a file instead of the console window; existing files will be overwritten. |
>> | Redirects a command's text output into a file instead of the console window, appending existing files. |
< | Directs the contents of a text file to a command's input; use in place of keyboard entry to automate interactive command-line applications. |
| | Redirects the output of a program or command to a second program or command (this is called a "pipe"). |
6.3.1. Examples
The following examples demonstrate some uses of wildcards, pipes, and
redirection:*.*
Specify all files with all extensions.
professor*.*
Specify all files (with filenames that begin with
"professor") with any extension.
chap??.doc
Specify all files named "chap"
followed by any two characters and with the doc
extension (e.g., chap01.doc, but not
chap1.doc or chap.doc).
dir ..
List all the files in the current directory's parent.
dir > c:\nutshell\mylist.txt
List all files in the current directory and
store this listing into a file called mylist.txt
rather than displaying it in the command prompt window. If the file
already exists, it will be overwritten.In addition to directing output to a file, you can direct to a
device, such as NUL (an electronic void). This is useful if you want
a command to run without sending output to the screen.
dir c:\windows >> c:\nutshell\mylist.txt
Add the directory listing of the files in the
c:\Windows directory to the end of the file
windows.txt.If the specified file doesn't exist, one is created.
If one does exist, the output from the command is added to it, unlike
with the > key, where the original contents are overwritten.
echo y | del *.*
Normally, the DEL command has no prompt. However, if you try to
delete all the files in a directory, del will
prompt you for confirmation. To automate this command, the output of
the ECHO command (here, just a "y"
plus a carriage return) is "piped"
into the input (commonly known as STDIN) of the DEL command.
del *.* < y.txt
Assuming y.txt contains only a letter
"y" followed by a carriage return,
this command has the same effect as the previous example.
sort /+12 < c:\nutshell\mylist.txt
To sort the lines in a text file
(c:\nutshell\mylist.txt) on the twelfth
character, the SORT command is fed input from the file. The output is
sent to the screen, not reordered in the file.
Keep in mind that not all commands handle wildcards in exactly the
same way. For example, dir * and dir
*.* list the same thing , but del *
will delete only files without an extension.