Windows 7 / Getting Started

Changing the Path

You can change the search path in two ways: First, you can set a new value for the path environment variable using the set command, as in this example:

set path=c:\batchfiles

Second, you can use the "shortcut" command, as shown here:

path c:\batchfiles

Both have the same effect:They set the environment variable path to c:\batchfiles, so CMD only looks for programs in this folder.This is probably not a good idea, though, because it removes the Windows folders from the path and makes all the standard command-line programs unavailable.

Note: The path and set commands only change environment variables for the current, running instance of the CMD program. If you open a new Command Prompt window, you are back to the original, default values.

If you want to make a permanent change to path or any other environment variable so that the change appears in all future CMD prompt windows, you need to make the change on the System Properties dialog box, as I discuss later in the tutorial under "Setting Default Environment Variables." Alternatively, you can use a script.

Most of the time, what you need to do is add a new folder to the existing path list, usually at the beginning, so that any batch files you write or new programs you install can be run just by typing their name.This is a particularly good time to use environment variable substitution and is such a common thing to do that it's a pattern.

Pattern

To add a directory to the beginning of the search path, use the following statement:

set path=directorypath;%path%

Here, directorypath should be the fully qualified folder name. Here's an example:

set path=c:\batchfiles;%path%

When CMD encounters this command-that is, if you type it at the command line or in a batch file- CMD first replaces the text %path% with the current value of PATH, so the command turns into this:

set path=c:\batchfiles;c:\windows\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; ...

Now, the new path starts with c:\batchfiles but still includes the standard Windows folders. If you place a batch file named test.bat in c:\batchfiles, and then type test on the command line, your batch file runs.

You could also add a new folder to the end of the search path with a statement like this:

set path=%path%;c:\batchfiles

The ordering only matters if there are versions of the same command in more than one folder in the path; the version in the first folder to be searched is the one that Windows runs.

Tip If you mess up the path with one of these commands and CMD stops working, just close the Command Prompt window and open another. This restores the path to the default setting.

Putting your own folders ahead of the Windows folders in the list can be a blessing or a curse. If you create a program or batch file with the same name as a standard Windows program, yours runs instead of the standard program. If this is what you want, great, but if not...the result can be confusing.

[Previous] [Contents] [Next]