As you’ve seen in this book, tweaking Windows once in a while requires that you fire up a Command Prompt session and then enter one or more commands. And while it’s true that getting the most out of Windows doesn’t require you to become a Command Prompt guru (insert sigh of relief here), it will help a lot if you’re at least comfortable in the not-so-friendly confines of the Command Prompt window.
To that end, this chapter provides you with just enough information to get you comfortable with the command prompt. You learn how to start a session, how to run DOS command, and I show you several useful techniques for working at the command prompt.
To take advantage of Command Prompt and all its many useful commands, you need to start a Command Prompt session. Windows (as usual) offers a number of ways to get to the command prompt:
• Select Start, All Programs, Accessories, Command Prompt.
• In Vista, open the Start menu, type comm
(or cmd
) into the Search box, and then press Enter.
• Press Windows Logo+R (or select Start, All Programs, Accessories, Run in Vista, or Start, Run in XP), type cmd
in into the Run dialog box, and click OK.
• Create a shortcut for %SystemRoot%system32cmd.exe
on your desktop (or some other convenient location, such as the taskbar’s Quick Launch toolbar) and then launch the shortcut.
• Reboot your computer, press F8 to display the Windows Vista Advanced Options Menu, and select the Safe Mode with Command Prompt item.
If you start the session by running the Command Prompt command, the session begins in your main user profile folder, as shown in Figure 36.1 (C:UsersPaul
, in this case). If you start the session by running cmd.exe
or by selecting the Safe Mode with Command Prompt startup item instead, the session begins in %SystemRoot%system32
(usually C:Windowssystem32
).
• You can also configure the Windows Folder file type to open Command Prompt in Windows Explorer’s current folder. To learn how, see “Open a Folder at the Command Prompt” p. 262.
If you’re running Windows Vista, once in a great while you not only need to start a Command Prompt session, but you need to start an elevated session. That’s because certain Windows commands and utilities require Administrator-level permission. This is normally handled by User Account Control, which prompts you for administrator credentials before letting you continue with the operation. However, this is usually not the case with operations launched from the command prompt.
For example, in Chapter W3, “Creating Your Own Custom Startup,” you learn how to use the BCDEDIT
tool to edit the Vista’s Boot Manager. This tool requires Administrator permissions, but you run it from the command prompt, so UAC doesn’t come into play. To use BCDEDIT
(or any Command Prompt utility or command that requires elevation) successfully, you need to run it within a Command Prompt session that has been elevated to Administrator status. Here’s how it’s done:
1. Select Start, All Programs, Accessories. (Alternatively, open the Start menu and type comm
into the Search box.)
2. Right-click Command Prompt and then click Run as Administrator. UAC prompts you for your credentials.
3. Enter your administrator credentials to continue.
In this case, the Command Prompt window still appears, but the title bar reads Administrator: Command Prompt
to remind you that you’re in an elevated session (see Figure 36.2).
CMD.EXE
SwitchesFor the methods that use the CMD
executable, you can specify extra switches after the cmd.exe
filename. Most of these switches aren’t particularly useful, so I’ll just show you the simplest syntax that you’ll use most often:
cmd [[/s] [/c | /k] command
]
/s |
Strips out the first and last quotation marks from the command , provided that the first quotation mark is the first character in command |
/c |
Executes the command and then terminates |
/k |
Executes the command and remains running |
command |
The command to run |
For example, if your ISP provides you with a dynamic IP address, you can often solve some connection problems by asking the ISP for a fresh address. You do that by running the command ipconfig /renew
at the command prompt. In this case, you don’t need the Command Prompt window to remain open, so you can specify the /c
switch to shut down the Command Prompt session automatically after the IPCONFIG
utility finishes:
cmd /c ipconfig /renew
On the other hand, you often either want to see the results of the command, or you want to leave the Command Prompt window open so that you can run other commands. In those cases, you use the /k
switch. For example, the following command runs the SET
utility (which displays the current values of the Windows environment variables) and then leaves the Command Prompt session running:
cmd /k set
Although many of the Windows accessories provide more powerful and easier-to-use replacements for nearly all commands, a few commands still have no Windows peer. These include the REN
command, as well as the many Command Prompt–specific commands, such as CLS, DOSKEY
, and PROMPT
.
How you run a command depends on whether it’s an internal or external command, and on what you want Windows to do after the command is finished. For an internal command, you have two choices: You can either enter the command in Command Prompt or include it as a parameter with cmd.exe
. As you saw earlier, you can run internal commands with cmd.exe
by specifying either the /c
switch or the /k
switch. If you use the /c
switch, the command executes and then the Command Prompt session shuts down. This is fine if you’re running a command for which you don’t need to see the results. For example, if you want to redirect the contents of drive C’s root folder in the text file root.txt
, entering the following command in the Run dialog box (for example) will do the job:
cmd.exe /c dir c: > root.txt
On the other hand, you might want to examine the output of a command before the Command Prompt window closes. In that case, you need to use the /k
switch. The following command runs DIR
on drive C’s root folder and then drops you off in Command Prompt:
cmd.exe /k dir c:
For an external command, you have three choices: enter the command at the command prompt, enter the command by itself from within Windows, or include it as a parameter with cmd.exe
.
To enter a command by itself from within Windows means launching the command’s file in Explorer, entering the command in the Run dialog box, or creating a shortcut for the command. For the latter two methods, you can embellish the command by adding parameters and switches. The problem with this method is that Windows automatically closes the Command Prompt window when the command completes. To change this behavior, follow these steps:
1. Find the command’s executable file in the %SystemRoot%System32
folder.
2. Right-click the executable file and then click Properties to display the command’s properties sheet.
3. Display the Program tab. (Note that this tab doesn’t appear for all commands.)
4. Deactivate the Close on Exit check box.
5. Click OK.
If you use the command line or the Run dialog box to enter your Command Prompt commands, you can easily tack on any extra parameters or switches you want to use to modify the command. If, however, you start an external command from Explorer, the command runs without any options. To modify how an external command operates, you can add parameters and switches by following these steps:
1. Use Windows Explorer to find the command’s executable file.
2. Right-click the executable file and then click Properties to display the command’s properties sheet.
3. Display the Program tab.
4. In the Cmd Line text box, add a space after the command and then add your parameters and switches. Figure 36.3 shows an example.
5. Click OK.
If you want to vary the parameters each time you run the command, add a space and a question mark (?) to the end of the command, like so:
%UserProfile%mem.exe ?
Each time you run the command (whether from Explorer or from the Run dialog box), Windows displays a dialog box similar to the one shown in Figure 36.4. Use the text box to type your switches and options, and then click OK.
Unlike the old DOS, you can work with long filenames within a Windows Command Prompt session. If you want to use long filenames in a command, however, you need to be careful. If the long filename contains a space or any other character that’s illegal in an 8.3 filename, you need to surround the long name with quotation marks. For example, if you run the following command, Windows will tell you that The syntax of the command is incorrect
:
copy Fiscal Year 2008.doc Fiscal Year 2009.doc
Instead, you need to enter this command as follows:
copy “Fiscal Year 2008.doc” “Fiscal Year 2009.doc”
Long filenames are, of course, long, so they tend to be a pain to type in Command Prompt. Fortunately, Windows offers a few methods for knocking long names down to size:
• In Explorer, drag a folder or file and drop it inside the Command Prompt window. Windows pastes the full pathname of the folder or file to the end of the prompt.
• In Explorer, hold down Shift, right-click the folder or file, and then click Copy as Path to copy the object’s full pathname to the Clipboard. Use the technique shown later in the “Pasting Text to the Command Prompt” section to paste the pathname into your Command Prompt session.
• If you’re trying to run a program that resides in a folder with a long name, add the folder to the PATH
. This technique enables you to run programs from the folder without having to specify the full pathname. (I talk about this in more detail in the next section.)
• Use the SUBST
command to substitute a virtual drive letter for a long pathname. For example, the following command substitutes drive Z for the current user’s Accessories folder:
subst z: “%UserProfile%AppDataRoamingMicrosoftWindowsStart Menu
ProgramsAccessories”
You can also use the command prompt to start Windows applications, launch documents, and even open folder windows. As with DOS programs, you start a Windows application by entering the name of its executable file.
This works fine if the executable file resides in the main Windows folder, because that folder is part of the PATH
. But most Windows applications (and even some Windows accessories) store their files in a separate folder and don’t modify the PATH
to point to these folders. Instead, the Registry has an AppPaths
key that tells Windows where to find an application’s files. Command Prompt can’t use the Registry-based application paths directly, but there’s a Windows command that can. This command is START
, and it uses the following syntax:
START [“title”] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/AFFINITY hex value] [/WAIT] [/B] [filename] [parameters]
When you use START
to launch a program, Windows checks not only the current folder and the PATH
, but also the Registry. For the Registry, Windows looks for an AppPaths
setting or a file type (if you entered the name of a document). For example, if you type wordpad
and press Enter at the command prompt, you get a Bad command or file name error
(unless you happen to be in the %Program Files%Windows NTAccessories
folder). If, however, you enter start wordpad
, WordPad
launches successfully.
Windows loads the DOSKEY
utility by default when you start any Command Prompt session. This useful little program brings a number of advantages to your command-line work:
• You can recall previously entered commands with just a keystroke or two.
• You can enter multiple commands on a single line.
• You can edit commands instead of retyping them.
The next three sections provide the details.
The simplest DOSKEY
feature is command recall. DOSKEY
maintains a command history buffer that keeps a list of the commands you enter. To scroll through your previously entered commands in reverse order, press the Up Arrow key; when you’ve done that at least once, you can change direction and run through the commands in the order you entered them by pressing the Down Arrow key. To rerun a command, use the arrow keys to find it and then press Enter.
Table 36.1 lists all the command-recall keys you can use.
DOSKEY
enables you to run multiple commands on a single line. To do this, insert the characters && between commands. For example, a common task is to change to a different drive and then run a directory listing. Normally, you’d do this with two separate commands:
e:
dir
With DOSKEY
, however, you can do it on one line, like so:
e:&&dir
Rather than simply rerunning a previously typed command, you might need to run the command again with slightly different switches or parameters. Rather than retyping the whole thing, DOSKEY
enables you to edit any recalled command line. You use various keys to move the cursor to the offending letters and replace them. Table 36.2 summarizes DOSKEY’s
command-line editing keys.
Command Prompt sessions don’t know about the Clipboard, so they don’t support the standard cut, copy, and paste techniques. However, there are methods you can use to share data between the command prompt and Windows applications. I spell them out in the next two sections.
The best way to copy text from the command prompt is to highlight the text you want and then copy it. The following procedure takes you through the required steps:
1. Make sure that the text you want to copy is visible in the Command Prompt window.
2. Pull down the window’s control menu and select Edit, Mark to put the window into Select mode. (You can also right-click the title bar and then select Edit, Mark.)
3. Use the mouse or keyboard to select the data you want to copy.
4. Pull down the window’s control menu and select Edit, Copy to copy the selected data to the Clipboard. (You can also either press Enter or right-click the title bar and then select Edit, Copy.)
5. Switch to the Windows application you want to use as the destination and position the insertion point where you want the copied data to appear.
6. Select Edit, Paste (or press Ctrl+V).
If you’ve sent some text to the Clipboard from a Windows application, it’s possible to copy the text into a Command Prompt session.
First, position the command prompt cursor at the spot where you want the pasted text to appear. Then pull down the window’s control menu and select Edit, Paste (or right-click the title bar and then select Edit, Paste).
18.118.16.229