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 other places in this book), certain symbols used on the command line have special meaning. Table 14-1 shows these special symbols and what they do. You must use them in conjunction with other commands (they don’t stand alone), and you can use them in the Command Prompt window, in Start → Run, and in an Address Bar.

Table 14-1. Special symbols on the command line

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,” later in this chapter.

..

Two dots represent the parent directory; see "cd or chdir,” later in this chapter.

Separates directory names, drive letters, and filenames. By itself, represents the root directory of the current drive.

\

Indicates a network location, such as \Joe-PC for a PC connected to your current network.

>

Redirects a command’s text output into a file instead of the Console window; if that file exists, it will be overwritten.

>>

Appends a command’s text output to the end of a file instead of the Console window.

<

Directs the contents of a text file to a command’s input; use with filter programs (such as sort) or 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).

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: utshellmylist.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. Other special device names include CON (the console input/output stream), LPTx (the parallel printer port, where x is from 1 to the number of ports you have), and COMx (serial ports, including Bluetooth and USB/serial ports, where x corresponds to the numbers shown in Device Manager).

dir c:windows >> c: utshellmylist.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, or standard input) 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 preceding example.

sort /+12 < c: utshellmylist.txt

To sort the lines in a text file (c: utshellmylist.txt) on the 12th 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.

Most of the following commands are not standalone applications, but rather internal functions of the Command Prompt (cmd.exe) application. This restricts their use only to the Command Prompt application. (They won’t be recognized by the Address Bar or by Start → Run.) Some items that are standalone programs but are normally used only in the Command Prompt window, such as xcopy.exe and move.exe, are listed here rather than in Chapter 4.

Note that before listing each individual command, I’m first offering more details on how to use the Command Prompt. Here is an alphabetical reference of entries in this chapter:

Attrib

find

rem

call

for

ren or rename

cd or chdir

goto

robocopy

Choice

if

set

clip

md or mkdir

shift

cls

mklink

sort

Command Prompt

more

time

copy

move

timeout

date

Path

type

del or erase

pause

ver

dir

prompt

where

echo

query

whoami

errorlevel

quser

xcopy

exit

rd or rmdir

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.111