Special Command Tricks
These techniques can let you accomplish in a single command what would otherwise take dozens of separate commands.
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.
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!
displays the message "Oops!" if the Copy command fails.
Finally, you can use parentheses to group commands. Then, you can use the other symbols in combination. For example:
C:\>(copy *.doc a: && del *.doc) || echo Oops!
Here, the files are copied and then deleted if the copy was successful. If either command fails, the message is displayed.
Redirection and piping
Redirection and piping are related techniques. Redirection lets you specify an alternative destination for output that will be displayed by a command or an alternative source for input that should be fed into a command. For example, you can save the results of an ipconfig /all command to a file named myconfig.txt like this:
C:\>ipconfig /all > myconfig.txt
Here, the greater-than sign (>) is used to redirect the command's console output.
If a command accepts input from the keyboard, you can use input redirection to specify a file that contains the input you want to feed to the command. For example, you can create a text file named lookup.txt with subcommands for a command such as nslookup. Then, you can feed those scripted subcommands to the nslookup command, like this:
C:\>nslookup < lookup.txt
Piping is a similar technique. It takes the console output from one command and feeds it into the next command as input. Piping is often used with special commands called filters, which are designed to read input from the console, modify the data in some way, and then write it to the console.
For example, suppose that you want to display the contents of a file named users.txt sorted into alphabetical order. You can use the Type command, which displays a file on the console, and then you can pipe the output into the Sort command, a filter that sorts its input and displays the sorted output on the console. The resulting command would look like this:
C:\>type users.txt | sort
The vertical bar is often called the pipe character because it's the symbol used to indicate piping.