The Search Path
CMD has a mechanism that lets you store text information in named variables called environment variables. Environment variables are used to control the behavior of CMD and can also be used to store information in batch file programs. Environment variables can be defined in batch files, on the command line, or by WSH programs, and many are predefined by Windows.
One of the more important environment variables is PATH, which holds a list of directories that CMD searches to find programs. PATH is a list of folder names separated by semicolons. Its default value looks something like this:
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\
This path list tells CMD that when you type a command, it should look for the command's program file in these directories, in precisely this order:
- The current directory, which CMD always searches first
- c:\windows\system32
- c:\windows
- c:\windows\system32\wbem
- c:\windows\system32\WindowsPowerShell\v1.0
If CMD finds the program file in one of these folders, it's run and no further folders are searched.This means that if there are command files or batch files with the same names in more than one search path folder, the one that is run when you type its name is the one whose directory appears earliest in the path.
Tip If the same program or batch file is in more than one folder in your search path, and the first one that CMD finds is not the one you want, there is a workaround. You can type a full pathname before the command to eliminate any ambiguity about which copy of the program you want to use. For example,
c:\windows\system32\ping
tells CMD exactly where to find the PING program.
Windows sets the PATH variable when it starts CMD, and you should be careful when changing it; if you remove the important Windows directories, CMD isn't be able to find many of the programs you want to use.
You can change the path to include your own directories using the path command. If you plan on writing your own batch files,WSH programs, or other application programs, it's a good idea to place them in a special folder of their own and then add that folder to the path. I show you how to do this in the next section.
In this tutorial:
- The CMD Command-Line
- CMD Versus COMMAND
- Running CMD
- Opening a Command Prompt Window with Administrator Privileges
- CMD Options
- Disabling Command Extensions
- Command-Line Processing
- Console Program Input and Output
- Using the Console Window
- I/O Redirection and Pipes
- Copy and Paste in Command Prompt Windows
- Command Editing and the History List
- Name Completion
- Enabling Directory Name Completion
- Multiple Commands on One Line
- Grouping Commands with Parentheses
- Arguments, Commas, and Quotes
- Escaping Special Characters
- Configuring the CMD Program
- The Search Path
- Changing the Path
- Predefined and Virtual Environment Variables
- Setting Default Environment Variables
- Built-in Commands
- Extended Commands
- Listing Files with the Dir Command
- Paginating Long Listings
- Printing Directory Listings
- Sorting Listings
- Locating Alternate File Streams
- Setting Variables with the Set Command
- Conditional Processing with the if Command
- Scanning for Files with the for Command
- Using the for Command's Variable
- Processing Directories
- Numerical for Loop
- Getting More Information