
578
Special Command Tricks
Wildcards
Wildcards are one of the most compelling reasons to use the command
prompt. With wildcards, you can process all the files that match a particular
naming pattern with a single command. For example, suppose that you have
a folder containing 500 files, and you want to delete all the files that contain
the letters Y2K and end with the extension .doc, which happens to be 50
files. If you open a My Documents window, you’ll spend ten minutes picking
these files out from the list. From a command window, you can delete them
all with the single command Del *Y2K*.doc.
You can use two wildcard characters: An asterisk stands for any number
of characters, including zero, and an exclamation point stands for just one
character. Thus, !Text.doc would match files with names like aText.
doc, xText.doc, and 4Text.doc, but not abcText.doc or just Text.doc.
However, *Text.doc would match any of the names mentioned in the
previous sentence.
Wildcards work differently in Windows than they did in MS-DOS. In MS-DOS,
anything you typed after an asterisk was ignored. Thus, ab*cd.doc was the
same as ab*.doc. In Windows, the asterisk wildcard can come before static
text, so ab*cd.doc and ab*.doc are not the same.
Chaining commands
You can enter two or more commands on the same line by separating the
commands with an ampersand (&), like this:
C:\>copy *.doc a: & del *.doc
Here, the Copy command copies all the .doc files to the A: drive. Then, the
Del command deletes the .doc files.
Although that may be convenient, it’s also dangerous. What if the A: drive
fills up so that all the files can’t be copied? In that case, the Del command
executes anyway, deleting the files that didn’t get copied.
A safer alternative is to use two ampersands, which says to execute the
second command only if the first command finishes successfully. Thus:
C:\>copy *.doc a: && del *.doc
Now, the Del command will be executed only if the Copy command succeeds.
You can also use two pipe characters (the pipe is the vertical bar character
that’s above the backslash on the keyboard) to execute the second command
only if the first command fails. Thus,
C:\>copy *.doc a: || echo Oops!
56_625873-bk07ch08.indd 57856_625873-bk07ch08.indd 578 9/21/10 10:41 PM9/21/10 10:41 PM