The command interpreter is a special kind of
application. When you open a command window, what you're really doing is
starting the command interpreter. The command interpreter accepts your
commands and does something with them. The command interpreter for
Windows is CMD.EXE. This application
is responsible for creating the command window, accepting your
configuration commands, and providing access to the built-in commands
such as the Dir command.
You can configure the command interpreter using five techniques in Windows. The first is to add command line switches to the CMD.EXE.
This approach configures the command interpreter as a whole and you
don't have much control over this particular change once the command
interpreter is running. Of course, you can have a shortcut for each
occasion that relies on different command line switch setups for each
task.
The second configuration method is to change the content of Config.NT. The Config.NT file appears in the \Windows\System32 directory and the command interpreter calls on it to configure the command window environment. The Config.NT
file changes the device drivers, number of files, the loading of the
Virtual DOS Machine into upper memory, and other configuration issues.
The Windows directory
can appear in many locations on a system. For example, it may not
install on the C drive, but could use the D drive instead. In addition,
the name can change some systems used Windows, some Win, some WinNT, and
others still other names. To find the Windows directory on the local
system, type Echo %SYSTEMROOT%
and press Enter. You'll see the location of the Windows directory
displayed at the command prompt. Once you know this information, you can
find all of the other resources you need on the local machine because
the Windows directory setup doesn't change much from system to system.
|
|
The third configuration method is to change the content of AutoExec.NT.
This file is actually a batch file that you can modify as you would any
other batch file.
The fourth
configuration method is the Program Information File (PIF). The
configuration information you provide for DOS applications using this
file directly affect their execution. In fact, the PIF provides a means
of specifying alternative Config.NT and AutoExec.NT files. Consequently, when your command line application requires a special environment in which to run, you can create it.
1. Using the CMD Switches
The command interpreter, CMD.EXE,
is the most important part of the command line because it affects
everything you do at the command line. A small change in the command
interpreter can make a significant change in the way your applications
run. The default command prompt setup assumes that you don't want to use
any of the command line switches and that you want to start in your
home directory.
If you used the Disk
Operating System (DOS) at some point, it's important to remember that
the command line switches that Windows supplies for the CMD.EXE
command interpreter in no way match what you used in the past.
Microsoft does make some command line switches available for
compatibility purposes. For example, the /X command line switch is the same as /E:ON, /Y is the same as /E:OFF, and /R is the same as /C.
The command interpreter ignores all other old switches; you need to use
the command line switches described in this section instead.
You might also
remember a few convenience features from the days of DOS that no longer
appear as part of Windows. For example, at one time you could create a
setup menu by using the [MENU] entry in Config.SYS. The Config.NT file doesn't support this setup. The only alternative is to create multiple Config.NT
files and assign them to applications as needed. In short, even though
the command interpreter does many of the same things that the DOS
version does, this command interpreter is different and you need to
proceed with caution about any assumptions you want to make. This
application uses the following syntax:
CMD [{/A | /U}] [/Q] [/D] [/E:{ON | OFF}] [/F:{ON |
OFF}] [/V:{ON | OFF}] [[/S] [{/C | /K}] string] [/T:FG]
The following list describes each of the command line arguments.
/C
stringPerforms the command specified by string
and then terminates the command interpreter session. Generally, you
won't get to see any application output using this technique unless the
application provides graphical output or you use redirection to save the
results in a file.
NOTE
When using either the /C or /K
command line switches, you can specify multiple commands by creating a
single string that contains all of the commands. Separate each command
using a double ampersand (&&). You must enclose the entire
string in double quotes. For example, "Dir *.DOC&&Dir *.TXT" would perform two Dir
commands. The first would search for any file with a DOC extension,
while the second would search for any file with a TXT extension.
/K
stringPerforms the command specified by string. The command window remains after execution ends so that you can see the application results.
/S Modifies the treatment of the command string used with the /C and /K command line switches. The command interpreter provides two methods for processing the command string. When you use the /S command line switch, the command processor views the string associated with the /C and /K
command line switch. It verifies that the first character is a quote
and removes it from the string. The command processor then looks for the
closing quote and removes it as well. You can use this option when the
presence of quotes causes problems executing the command. The command
interpreter also strips the quotations marks when you:
Use any of the following special characters within the string: & < >( ) @ ^ | Include one or more white space characters Include an executable filename as part of the string Use more than one set of quotes in the string
/Q Turns off echo. Echo is the output of the command interpreter that tells you which command is running.
/D Disables the execution of AutoRun commands from the registry.
/A Specifies the output of internal commands to a pipe or file using American National Standards Institute (ANSI) characters.
/U Specifies the output of internal commands to a pipe or file using Unicode characters.
/E:ON Enables the command extensions. The command extensions provide added functionality for these commands: Assoc, Call, ChDir (CD), Color, Del (Erase), EndLocal, For, FType, GoTo, If, MkDir (MD), PopD, Prompt, PushD, Set, SetLocal, Shift, and Start (also includes changes to external command processes).
NOTE
You might notice that some commands appear in parentheses. For example, the ChDir command appears before the (CD)
command in parentheses. The two commands are equivalent. You can use
whichever form you want. The parentheses don't show a preference, simply
an alternative.
/E:OFF Disables command extensions.
/F:ON Enables file and
directory name completion characters. File and directory completion
allow speed typing at the command line. For example, if you want to type
Dir Temp, using directory or file completion, you could type Dir T,
and then press Ctrl+D (for a directory) or Ctrl+F (for a file). The
command interpreter automatically completes the directory or filename
for you. If you type in a partition string that doesn't match any
entries, the command interpreter beeps to signify that the entry is
incorrect. When the command interpreter sees multiple entries that could
match the entry you provide, it displays the first entry in the list.
You cycle through the entries by pressing Ctrl+D or Ctrl+F again. Use
the Shift+Ctrl+D and Shift+Ctrl+F control key combinations to move
backward through the list of choices. You can change the control
characters that this feature uses by changing the associated registry
entry. You must enclose any file or directory names that begin with
special characters in quotes. These characters include <space>
& ( ) [ ] { } ^ = ; ! ' + , ` ~.
/F:OFF Disables file and directory name completion characters (see the /F:ON command line switch for details).
/V:ON Enables
delayed environment variable expansion. The expansion relies on the
exclamation mark (!) as the delimiter. Consequently, supplying !MyVar! at the command line would expand (display the value of) MyVar at execution time.
/V:OFF Disables delayed environment variable expansion.
NOTE
Vista and Server Core continue to support the /T
command line switch, even though it doesn't appear with the CMD
executable help. It's important to realize that Microsoft often gets rid
of undocumented command line switches, so you might not have access to
this feature in a future release.
/T: FGSets the foreground (F) and background (G)
colors. You must place the values together, without any space between.
The following list tells you which colors you can use at the command
prompt, along with their associated color number.
- 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
- F—Bright white
Combine the command line interpreter with the Start command to create additional windows as you need them. To create an additional basic command interpreter, type Start /Separate CMD.EXE and press Enter. Notice the positioning of the /Separate command line switch—it belongs to Start, so you place it with Start. If you wanted to create a new command line interpreter with a blue background and green lettering, you'd type Start /Separate CMD.EXE /T:1A and press Enter. Again, notice the positioning of the /T command line switch—it belongs to CMD.EXE, so you place it there. When you finish using the new command line interpreter, type Exit and press Enter.
1.1. Working with the Command Interpreter in the Registry
Many of the command line behaviors depend on registry settings. You can find these settings in the HKEY_CURRENT_USER\Software\Microsoft\Command Processor key for the local user and HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
for everyone using the same machine. The command interpreter looks for
these registry settings when you don't provide an appropriate command
line switch. If you don't see the registry entry, then the command
interpreter uses a default setting. Local user settings always override
the machine settings, and command line switches always override the
registry settings. Here are the registry settings and their meanings.
AutoRun Defines the command that you want the command interpreter to run every time you open a command prompt. This value is of type REG_SZ or REG_EXPAND_SZ.
Simply provide the executable name along with any command line switches
that the executable may require. As with the string for the /C and /K command line switches, you can separate multiple commands using a double ampersand (&&).
EnableExtensions Specifies whether the command interpreter has extensions enabled. See the /E:ON command line switch description for a list of the applications that this entry affects. This value is of type REG_DWORD. Set it to enabled using a value of 0x1 and disabled using a value of 0x0.
CompletionChar Defines the file completion character (see the /F:ON
command line switch for details). The default character is Ctrl+F
(0x06). Use a value of a space (0x20) to disable this feature since the
space isn't a valid control character. This value is of type REG_DWORD.
PathCompletionChar Defines the directory completion character (see the /F:ON
command line switch for details). The default character is Ctrl+D
(0x04). Use a value of a space (0x20) to disable this feature since the
space isn't a valid control character. This value is of type REG_DWORD.
DelayedExpansion Specifies whether the command interpreter uses delayed variable expansion. See the /V:ON command line switch description for additional information. This value is of type REG_DWORD. Set it to enabled using a value of 0x1 and disabled using a value of 0x0.
1.2. Understanding Command Extensions
Command
extensions are additional processing that the command interpreter
provides for certain commands. The effects vary by command, but
generally the commands receive additional functionality. In some cases,
such as the del (erase)
command, the extensions simply change the way the command works. The
following list describes the command extension changes to each of the
affected commands.
Assoc Microsoft hasn't documented how command extensions change the Assoc
command. Even though Microsoft lists it as one of the commands that
changes with command extensions, there isn't any obvious difference at
the command line.
Call Accepts a label
as the target for a call (rather than a filename as normal). This
feature means that you can transfer control from one portion of a batch
file to another. Using extensions means that you can call the label
using call :Label Arguments. Notice that you must precede the label with a colon.
Chdir (CD) Displays the
directory names precisely as they appear on your hard drive. For
example, if a directory name has a space, you'll see the space when you
change directories. Capitalization is also the same. A directory name
that appears with an initial capital letter in Windows Explorer also
appears that way at the command prompt. In addition, when you turn off
the command extensions, the command doesn't treat spaces as delimiters.
Consequently, you don't need to surround directory names with spaces or
with quotes in order to obtain the correct results from this command.
Color Microsoft hasn't documented how command extensions change the Color
command. Even though Microsoft lists it as one of the commands that
changes with command extensions, there isn't any obvious difference at
the command line.
Del (Erase) Changes the way the /S
command line switch works. The command shows you just the files that it
deletes, rather than showing you all of the files, including those that
it couldn't find.
EndLocal Restores the command extension settings to their state before calling the SetLocal command. Normally, the SetLocal command doesn't save the state of the command extensions.
For Implements an expanded number of For
command options. When working with directories, you process
directories, rather than a list of files within a directory, using this
call: for /D {%% | %}Variable in (Set) do Command [CommandLineOptions].
You can also perform recursive processing of a directory tree. Using
this feature means that a single command can process an entire tree,
rather than using individual commands to process a branch. Use the for /R [[Drive:]Path] {%% | %}Variable in (Set) do Command [CommandLineOptions]
command line syntax to perform recursion. It's also possible to iterate
through a range of values, similar to the functionality of the For loop used in higher level languages, using this command syntax: for /L {%% | %}Variable in (Start#,Step#,End#) do Command [CommandLineOptions]. Variable substitution is another useful feature that using command extensions provides . Finally, you can perform complex
file parsing and iteration with the command extension in place.
FType Microsoft hasn't documented how command extensions change the FType
command. Even though Microsoft lists it as one of the commands that
changes with command extensions, there isn't any obvious difference at
the command line.
Goto Defines a special label called :EOF. If you define a Goto command in a batch file with the :EOF
label, the system transfers control to the end of the current batch
file and exits. You don't need to define the label in the batch file to
make this feature work.
If Defines additional comparison syntax that makes the If command considerably more flexible.
MkDir (MD) Lets you
create intermediate directories with a single command. For example, you
could define an entire subdirectory structure using MD MyDir/MySub1/MySub2. If MyDir doesn't exist, the system creates it first, then MySub1, and, finally, MySub2. Normally, you'd need to create each directory separately and use the CD command to move to each lower level to create the next subdirectory.
PopD Removes any drive letter assignment made by the PushD command.
Prompt Supports additional prompt characters. The $+ character adds one or more plus signs (+) to the command prompt for every level of the PushD command. Using this feature lets you know how many levels of redirection the PushD command has saved on the stack and how many more times you can use the PopD command to extract them. The $m
character adds the remote name associated with a drive to the command
prompt. The command prompt doesn't display any additional information
for local drives.
PushD Allows you to push network paths onto the stack as well as local drive letters and path information.
Set Displays all currently defined environment variables when you use the Set
command alone. Displays the specified environment variable when you
supply an environment variable name, but not an associated value. If you
supply only a partial variable name, the Set command displays all of the variables that could match that name.
SetLocal Allows the SetLocal command to enable or disable command extensions as needed to meet specific language requirements.
Shift Supports the /N command line option, which lets the Shift command shift variables starting with the nth variable. For example, if you use Shift /2 at the command line, then variables %0 and %1 are unaffected by the shift, but variables %3 through %9 receive new variable input.
Start Microsoft hasn't documented how command extensions change the Start
command. Even though Microsoft lists it as one of the commands that
changes with command extensions, there isn't any obvious difference at
the command line.
|