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
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).
For information about special operators for chaining commands and redirection, see http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx and http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx.
The following examples demonstrate some uses of wildcards, pipes, and redirection:
Specify all files with all extensions.
Specify all files (with filenames that begin with professor) with any extension.
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).
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. 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:\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, 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:\nutshell\mylist.txt
To sort the lines in a text file (c:\nutshell\mylist.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:
ren or rename
cd or chdir
md or mkdir
del or erase
rd or rmdir
Command Prompt: \windows\system32\cmd.exe
The Windows Vista command-line interface, commonly referred to as a DOS box or Command Prompt.
As explained earlier in this chapter, the Command Prompt (see Figure 14-5) is a simple application in which you type commands rather than pointing and clicking. Although the Command Prompt is sparse and may be somewhat intimidating to new users, it carries out several very important functions in Windows Vista, including access to otherwise inaccessible programs and utilities and even some advanced file management functions.
Figure 14-5. Using the DIR command to view the contents of the current directory in the Command Prompt
Cmd accepts the following parameters:
When used with /c or /k, specifies a command to be carried out when the Command Prompt window is first opened. You can specify multiple commands here if you separate them with &&, and string, as a whole, is surrounded by quotation marks. String must be the last parameter on the command line.
Carries out the command specified by string and then stops.
Carries out the command specified by string and continues.
Strips any quotation marks in string. Type cmd /? for details.
Turns the echo off; see "echo," later in this chapter.
Disables execution of AutoRun commands. Without /d, any programs or commands listed in the Registry keys, HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun and HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun, are executed every time a Command Prompt window is opened.
Formats all Command Prompt output so that it is American National Standards Institute (ANSI)-compliant.
Formats all Command Prompt output so that it is Unicode-compliant.
Enables or disables command extensions (the default is on). Turn off command extensions to disable certain advanced features of the commands discussed in this chapter.
Enables or disables file and directory name completion (the default is off). Type cmd /? for details.
Enables or disables delayed environment variable expansion (the default is off). Type cmd /? for details.
Sets the foreground and background colors (f and g, respectively) of the Command Prompt window. The single-digit values for f and g are as follows: 0 = Black, 1 = Blue, 2 = Green, 3 = Aqua, 4 = Red, 5 = Purple, 6 = Yellow, 7 = White, 8 = Gray, 9 = Light blue, A = Light green, B = Light aqua, C = Light red, D = Light purple, E = Light yellow, and F = Bright white.
Also included with Windows Vista is command.com, the command prompt used in Windows 9x/Me. It's used similarly to cmd.exe but has limited support of long filenames and other Windows Vista features. Command.com is included for legacy purposes only and should be avoided; cmd.exe is the preferred command prompt in Windows Vista.
If you need to play an old DOS game, take a look at DOSBox (http://dosbox.sourceforge.net), which not only emulates DOS well, but also supports sound in older games.
Change or view the attributes of one or more files or folders.
Attrib allows you to change the file and folder attributes from the command linesettings otherwise available only in the Properties window of a file or folder. You can think of the attributes as switches, independently turned on or off for any file or group of files. The individual attributes are as follows:
Turn on the read-only attribute of a file or folder to protect it from accidental deletion or modification. If you attempt to delete a read-only file, Windows will prompt you before allowing you to delete it. Different applications handle read-only files in different ways; usually you will not be allowed to save your changes to the same filename.
The archive attribute has no effect on how a file is used, but it is automatically turned on when a file is modified or created. It is used primarily by backup software to determine which files have changed since a backup was last performed; most backup programs turn off the archive attribute on each file that is backed up.
Files with the system attribute are typically used to boot the computer. There's little reason to modify a file with the system attribute, or to ever turn on or off the system attribute for any file. If you turn off the system attribute of an important file, it may stop the file from working. See "Notes," later in this section, for information on displaying or hiding system files.
To hide any file or folder from plain view in Explorer or on the Desktop, turn on its hidden attribute. See "Notes," later in this section, for more information.
To hide a file in Explorer, right-click on it, select Properties, and turn on the hidden option. To hide the same file using the command line, type:
attrib +h filename
where filename is the full path- and filename of the file to change. To specify multiple files, include a wildcard, such as *.* (for all files) or *.txt (for all files with the .txt filename extension). Note the use of the plus sign (+) to turn on an attribute; use the minus sign (-) to turn it off. For example, to turn off the hidden attribute and simultaneously turn on the archive attribute, type:
attrib -h +a filename
To display the attributes of a file or a group of files in Explorer, select Details from the View menu. Then, select Choose Details from the View menu and turn on the Attributes option. To display the attributes of a file or a group of files on the command line, type:
where filename is the full path- and filename(s) of the files you want to view. Omit filename to display the attributes of all the files in the current folder. If filename is not used, or if it contains wildcards (in other words, if the command is intended to act on more than one file), you can use the /s option to further include the contents of all subfolders of the current folder. The /d option instructs Attrib to act upon folders as well as files, but it has meaning only if you use it in conjunction with the /s parameter.
By default, files with the system or hidden attributes are not shown in Explorer. To display system and hidden files, go to Explorer Organize Folder and Search Options View and select "Show hidden files and folders."
Attrib does not let you change the Advanced attributes, such as those concerned with indexing, compression, or encryption.
cd or chdir
Display the name of, or change, the current working directory (folder).
cd [/d] [directory]
chdir [/d] [directory]
With no arguments, cd displays the full pathname of the current directory. Given the pathname of an existing directory, it changes the current directory to the specified directory.
If directory is on a different drive (for example, if the current directory is c:\dream and you type cd d:\nightmare), the current working directory on that drive is changed, but the current working drive is not. To change the current drive, use the /d parameter, or simply type the letter followed by a colon, by itself, at the prompt (see the following examples).
Pathnames can be absolute (including the full path starting with the root) or relative to the current directory. A path can be optionally prefixed with a drive letter. The special path .. refers to the parent of the current directory.
If the current drive is C:, make c:\temp\wild the current directory:
Note how the current working directory is displayed in the prompt. If the current directory is c:\temp, all that is necessary is:
Change to the parent directory:
Change to the root directory of the current drive:
Change to another drive:
C:\>cd /d d:\
The chdir and cd commands are functionally identical.
The "current working directory" has meaning only in the current command prompt session and any other command prompts or applications launched from that window. If you open a new command prompt window, it will start over with its default (usually your home directory, but you can set it in the properties of the Windows Shortcut).
The current directory is shown in the prompt; see "prompt," later in this chapter, for information on changing the information displayed.
Redirects the output of command-line tools to the Windows Clipboard, where the text can then be pasted into another program.
[Command] | clip
clip < [filename.txt]
clip is extremely useful for when you want to capture output from a command on the command line so that you can paste it into an application. A simple use would be to print out the listing of a directory: use the dir command along with clip and then paste the output from the Clipboard into a text editor, where you can print it. Note that when you use the command, the output is suppressed in the command box. So, for example, when you type dir | clip, the directory listing will not be displayed but will be placed into the Clipboard. You can also use clip to copy the entire text from a text file to the Clipboard. You can't, however, use it to copy the contents of other kinds, such as .doc files.
Copy information about currently logged-on users to the Clipboard:
quser | clip
Copy the text of the file newinfo.txt to the Clipboard. Note that the file has to be in your current directory:
clip < newinfo.txt
clip is new to Windows Vista.
Clear the Command Prompt window and buffer, leaving only the Command Prompt and cursor.
Type cls at the prompt to clear the screen and the screen buffer (see "Using the Command Line," earlier in this chapter), which is useful for privacy concerns or simply reducing clutter.
The difference between using cls and simply closing the current Command Prompt window and opening a new one is that your working environment (such as the current directory) is preserved with cls.
cls is also useful in complex batch filesfor clearing the screen after one set of interactions or command output. The name cls (Clear Screen) refers to the old days when DOS owned the whole screen.
copy makes a complete copy of an existing file. If another file by the same name exists at destination, you will be asked whether you want to overwrite it.
Omit the destination to copy the specified files to the current working directory. If the file (or files) to be copied is in a different directory or on a different disk, you can omit the destination filename. The resulting copy or copies will have the same name as the original.
You can use the special device name con (or con:) in place of either the source (or destination) filename to copy from the keyboard to a file (or from a file to the screen).
copy accepts the following parameters and options:
Specifies that the file to copy is in ASCII format.
Specifies that the file to copy is a binary file.
Verifies that new files are written successfully by comparing them with the originals.
Suppresses prompting to confirm that you want to overwrite an existing destination file.
Enables prompting to confirm that you want to overwrite an existing destination file with the same name (default).
Allows the new file to be created as a decrypted file (NTFS volumes only).
When copying a symbolic link (see "mklink," later in this chapter), copies the file as a link rather than making a couple of the source file (the default behavior).
Copies the file using the short filename that Vista generated for compatibility with DOS, old versions of Windows, and devices that can't support long filenames on their flash memory.
Copies networked files in restartable mode. If the network connection is lost during copying (if the server goes offline and severs the connection, for example), /z will resume the copying after the connection is reestablished.
Copy the file temp.txt from C:\ to d:\files (all three examples do the same thing):
The copy command is easier to use, but xcopy (discussed later in this chapter) is more powerful and flexible.
It is also possible to use the copy command to concatenate (combine) files. To concatenate files, specify a single file for the destination but multiple files for the source (using wildcards or file1+file2+file3 format):
copy mon.txt+tue.txt+wed.txt report.txt
You can specify a relative or absolute path (including disk names and/or Universal Naming Convention [UNC] paths), or use a simple filename. When concatenating, if no destination is specified, the combined files are saved under the name of the first specified file.
When attempting to concatenate files, copy expects ASCII files by default, so in order to concatenate binary files, you need to use the /b option. The reason for this is that binary files typically contain one or more bytes outside the normal ASCII printable range (i.e., 32 through 127).
You also can use the con device (console) in conjunction with copy. To create a new text file by typing its contents directly, first enter:
C:\>copy con mystuff.txt
Then type the text to be saved into the file. When you're done, type Ctrl-Z and press Enter. All text typed from the keyboard in this example is then saved as mystuff.txt.
Here's how to copy the contents of the file mystuff.txt to the screen (see also "type," later in this chapter):
C:\>copy mystuff.txt con
Binary file copying is assumed for normal copying, but you should use the /b option when appending one binary file to another, as in:
C:\>copy file1+file2 newfile /b
By default, when concatenating, both the source and destination files are assumed to be ASCII format, because binary files can seldom be usefully concatenated due to internal formatting.
You can substitute a device (e.g., COM1) for either the source or the destination. The data is copied in ASCII by default.
copy doesn't copy files that are 0 bytes long; use xcopy to copy these files.
copy, move, and xcopy will prompt you before overwriting an existing file, unless you specify a command-line parameter instructing them to do otherwise. To change the default, set the copycmd environment variable to /y. To restore the default behavior, set copycmd to /-y. See "set," later in this chapter, for details.
Display or set the system date.
date [/t | date]
date is essentially a holdover from the very early days of DOS when the user was required to enter the system date and time every time the computer was started. Now it's essentially included as a way to set the date from the command line; the preferred method is to use Control Panel [Clock, Language and Region] Date and Time.
If you type date on the command line without an option, the current date setting is displayed and you are prompted for a new one. Press Enter to keep the same date.
date accepts the following options:
Specifies the date. Use the mm-dd- [ yy ] yy format. Values for yy can be from 80 through 99; values for yyyy can be from 1980 through 8907. Separate month, day, and year with periods, hyphens, or slashes.
Displays the current date without prompting for a new one. You can use this if you need to append a timestamp to the end of a file, as in date /t >> logfile.txt.
The date format depends on settings in Control Panel [Clock, Language, and Region] Regional and Language Options.
Windows records the current date for each file you create or change. This date is listed next to the filename in the dir directory listing.
You can change the date display format for most applications in Control Panel [Clock, Language, and Region] Change the date, time, or number format Customize this format, but this doesn't affect the output of the DOS date command.
You use the del command to delete one or more files from the command line without sending them to the Recycle Bin.
The del options are:
Specifies the file(s) to delete. If you do not specify the drive or path, the file is assumed to be in the current directory. You can use standard * and ? wildcards to specify the files to delete.
Prompts for confirmation before deleting each file.
Forces deletion of read-only files.
Deletes specified files in all subdirectories (when using wildcards).
Quiet mode; do not prompt if filename is *.*.
Selects files to delete based on attributes (read-only, hidden, system, or archive). See "Attrib," earlier in this chapter, for more information on attributes.
Delete the file myfile.txt in the current directory:
Delete the file myfile.txt in the c:\files directory:
Delete all files with the pattern myfile.* (e.g., myfile.doc, myfile.txt, etc.) in the current directory, but prompt for each deletion:
C:\>del c:\files\myfile.* /p
The del and erase commands are functionally identical.
Using the del command to delete a file does not move it to the Recycle Bin. In other words, you can't get a file back once you use the del command, unless you have a special "unerase" disk recovery utility.
Display a list of files and subdirectories in a directory (folder).
Without any options, dir displays the disk's volume label and serial number, a list of all files and subdirectories (except hidden and system files) in the current directory, file/directory size, date/time of last modification, the long filename, the total number of files listed, their cumulative size, and the free space (in bytes) remaining on the disk.
If you specify one or more file or directory names (optionally including drive and path, or the full UNC path to a shared directory), information for only those files or directories will be listed.
You can use wildcards (* and ?) to display a subset of files and subdirectories in a given location.
dir accepts the following options:
Displays only files with/without specified attributes (using - as a prefix specifies "not," and a colon between the option and attribute is optional). See "Attrib," earlier in this chapter, for more information on attributes.
Use bare format (no heading information or summary). Use with /s to list all files in the current directory and subdirectories.
Displays the thousand separator in file sizes. This is the default; use /-c to disable display of the separator.
Same as /w, except files are sorted vertically.
Lists files in a Unix-like display, where filenames are shown on the right. This is the default view.
Lists files in sorted order (using - as a prefix reverses the order, and a colon between the option and attribute is optional):
By date and time (earliest first)
By extension (sorted alphabetically)
Group directories first
By name (sorted alphabetically)
By size (smallest first)
Pauses after each screenful of information; press any key to continue.
Displays the owner of each file.
Includes all files in all subdirectories, in addition to those in the current directory.
Controls which time is used when sorting:
Last modified (written)
Wide list format. File- and directory names are listed in columns and sorted horizontally. The actual number of columns varies based on the length of the longest filename and the screen width. Use /d instead to sort vertically.
Include the "short" 8.3 versions of long filenames. For example, Sam's File.txt has an alternate filename, samsfi~1.txt, to maintain compatibility with older applications.
Display the listed years as four digits. By default, two-digit years are displayed.
Display all files in the current directory:
Display all files in the current directory that end with the .txt extension:
Display all files, listing years in four digits and pausing for each screenful:
C:\>dir /4 /p
Display all files, sorted by date and time, latest first:
Display only directories:
List all files on disk, sorted by size, and store output in the file allfiles.txt:
C:\>dir \ /s /os > allfiles.txt
List the contents of the shared folder cdrom on machine bubba:
To change the default sort order, set the dircmd environment variable to the same value you'd use with the /o parameter. See "set," later in this chapter, for details.
When using a redirection symbol (>) to send dir output to a file or a pipe (|) or to send dir output to another command, you may want to use /b to eliminate heading and summary information.
dirfilename/b /s acts as a kind of "find" command, which looks in all subdirectories of the current directory. For example:
One of Windows Explorer's weaknesses is that there's no way to print a directory listing or save a directory listing into a file. However, the dir command with some clever redirects will do the job.
To print out a sorted directory listing of all files in the Windows directory:
C:\> dir c:\windows /oa > filename
C:\> notepad /p filename
To create a file containing the directory listing of the same directory:
C:\>dir c:\windows /oa > c:\myfiles\windows.txt
Files and folders that are hidden (see "Attrib," earlier in this chapter) will not show up in dir listings by default. However, if you know the name of a hidden directory, nothing is stopping you from displaying a listing of the contents in that directory. You can also use dir /a.
Display a string of text; turn command echoing on or off.
echo [on | off | message]
echo is typically used with other commands or in a batch file to display text on the screen. It's also used to control command echoing from batch files.
You can use the following options with echo:
on | off
By default, each command in a batch file is echoed to the screen as it is executed; echo on and echo off toggle this feature. To turn echoing off without displaying the echo off command, use @echo off. The @ symbol in front of any command in a batch file prevents the line from being displayed.
Types the message you'd like displayed to the console (screen).
To display an ordinary message, use the following:
echo Hello World!
To display a blank line, use one of the following (both are equivalent):
(Note the absence of the space between the echo command and the punctuation; you can also use a colon, semicolon, square brackets, backslash, or forward slash.)
One handy use of echo is to answer y to a confirmation prompt such as the one del issues when asked to delete all the files in a directory. For example, if you wanted to clear out the contents of the \temp directory from a batch file, you could use the following command:
echo y | del c:\temp\*.*
echo y | if exists c:\temp\*.* del c:\temp\*.*
This construct works because the pipe character takes the output of the first command and inserts it as the input to the second.
You can use echo to announce the success or failure of a condition tested in a batch file:
if exist *.rpt echo The report has arrived.
It's a good idea to give users usage or error information in the event that they don't supply proper arguments to a batch file. You can do that as follows:
if (%1) == ( ) goto usage
. . .
echo You must supply a filename.
End the current Command Prompt session and close the window.
exit [/b] [exitcode]
Typing exit has the same effect as closing the Command Prompt window with the [x] button.
exit accepts the following options:
If you use exit from within a batch file, it will close the current Command Prompt window. Specify /b to exit the batch file but leave cmd.exe running.
Specifies a numerical "exit code" number that is passed to the application or process that launched the Command Prompt or started the batch file. exitcode is typically used when one batch file runs another batch file and wishes to report to the "parent" batch file whether it was successful.
If you start a new Command Prompt session by typing cmd in an open Command Prompt window, exit will end that session. However, because the "parent" session is still active, the window won't close until you type exit again.
After searching the specified files, find displays any lines of text that contain the string you've specified for your search. find is useful for searching for specific words (strings) in files.
The find options are:
The text to look for, enclosed in quotation marks.
The file(s) in which to search. Wildcards (*, ?) are supported, and you can specify multiple filenames as long as you separate them with commas. If filename is omitted, find searches text typed at the prompt or piped from another command via the pipe character (|), as described in "Wildcards, Pipes, and Redirection," earlier in this chapter.
Displays only the count of lines containing the string.
Ignores the case of characters when searching for the string.
Displays line numbers with the displayed lines.
Displays all lines not containing the specified string.
Includes files with the offline attribute set (that otherwise would be skipped).
Search for redflag in myemployees.txt:
C:\>find "redflag" myemployees.txt
Count occurrences of the word deceased in myemployees.txt:
C:\>find /c "deceased" myemployees.txt
Search the current directory for the string "cls" in all .bat files and