Working with the Command Path

 < Day Day Up > 



The Microsoft Windows operating system uses the command path to locate executables. The types of files that Windows considers to be executables are determined by the file extensions for executables. File extensions can also be mapped to specific applications using file associations. The two sections that follow discuss techniques for working with the command path, file extensions, and file associations.

Managing the Command Path

You can view the current command path for executables by using the PATH command. Start a command shell, type path on a line by itself, and press Enter. If you’ve installed the Windows Support Tools and the Windows Resource Kit, the results should look similar to the following:

PATH=C:\Program Files\Windows Resource Kits\Tools\;C:\Program 
Files\Support Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\
System32\Wbem

Note

Observe the use of the semicolon (;) to separate individual paths. The command shell uses the semicolon to determine where one file path ends and another begins.

The command path is set during logon using system and user environment variables, namely the %PATH% variable. The order in which directories are listed in the path indicates the search order used by the command line when looking for executables. In the previous example, the command line searches in this order:

  1. C:\Program Files\Windows Resource Kits\Tools\

  2. C:\Program Files\Support Tools\

  3. C:\Windows\System32

  4. C:\Windows

  5. C:\Windows\System32\Wbem

You can permanently change the command path in the system environment using the SETX command. (The SETX command is a native external command in Windows Server 2003, but is available in Windows XP Professional only after the Windows XP Professional version of the Windows Resource Kit has been installed from the operating system CD-ROM.) For example, if you use specific directories for scripts or applications, you may want to update the path information. You can do this by using the SETX command to add a specific path to the existing path, such as setx PATH “%PATH%;C\Scripts”.

Note

Observe the use of the quotation marks and the semicolon (;). The quotation marks are necessary to ensure that the value %PATH%;C:\Scripts is read as the second argument for the SETX command. And, as discussed previously, the semicolon is used to specify where one file path ends and another begins.

In this example, the directory C:\Scripts is appended to the existing command path and the sample path listed previously would be modified to read as follows:

PATH=C:\Program Files\Windows Resource Kits\Tools\;C:\Program 
Files\SupportTools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS
\System32\Wbem;C:\Scripts

Don’t forget about the search order that Windows uses. Because the paths are searched in order, the C:\Scripts directory will be the last one searched. This can sometimes slow the execution of your scripts. To help Windows find your scripts faster, you may want C:\Scripts to be the first directory searched. In this case, you could set the command path using the command

setx PATH "C:\Scripts;%PATH%" 

Be careful when setting the command path. It is easy to overwrite all path information accidentally. For example, if you don’t specify the %PATH% environment variable when setting the path, you will delete all other path information. One way to ensure that you can easily recreate the command path is to keep a copy of the command path in a file. To write the current command path to a file, type path > orig_path.txt. To write the command path to the command-shell window, type path.

Now you have a listing or a file that contains a listing of the original command path. Not only does the path command list the current command path, it also can be used to set the command path temporarily for the current command shell. For example, type path %PATH%;C:\Scripts to append the C:\Scripts directory to the command path in the current command shell.

Managing File Extensions and File Associations

File extensions are what allow you to execute commands by typing just their command name at the command line. Two types of file extensions are used:

  • File extensions for executables Executable files are defined with the %PATHEXT% environment variable. You can view the current settings by typing set pathext at the command line. The default setting is PATHEXT=.COM;
    .EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH. With this setting, the command line knows which files are executable and which files are not, so you don’t have to specify the file extension at the command line.

  • File extensions for applications File extensions for applications are referred to as file associations. File associations are what enable you to pass arguments to executables and to open documents, spreadsheets, or other application files by double-clicking their file icon. Each known extension on a system has a file association that can be viewed by typing assoc followed by the extension, such as assoc.exe. Each file association in turn specifies the file type for the file extension. This can be viewed by typing the FTYPE command followed by the file association, such as ftype exefile.

With executables, the order of file extensions sets the search order used by the command line on a per-directory basis. Thus, if a particular directory in the command path has multiple executables that match the command name provided, a .com file would be executed before a .exe file and so on.

Every known file extension on a system has a corresponding file association and file type—even extensions for executables. In most cases, the file type is the extension text without the period followed by the keyword file, such as cmdfile, exefile, or batfile, and the file association specifies that the first parameter passed is the command name and that other parameters should be passed on to the application.

You can look up the file type and file association for known extensions using the ASSOC and FTYPE commands. To find the association, type assoc followed by the file extension that includes the period. The output of the ASSOC command is the file type. So if you type ftype association (where association is the output of the ASSOC command), you’ll see the file type mapping. For example, if you type the following command to see the file associations for .exe executables: assoc .exe, you then type ftype exefile.

You’ll see the file association is set to

exefile="%1" %*

Thus, when you run an .exe file, Windows knows the first value is the command that you want to run and anything else provided are parameters to pass along.

Tip

File associations and types are maintained in the Windows Registry and can be set using the ASSOC and FTYPE commands respectively. To create the file association, type assoc followed by the extension setting, such as assoc .pl=perlfile. To create the file type, set the file type mapping, including how to use parameters supplied with the command name, such as perlfile=C:\Perl\Bin\Perl.exe “%1” %*. To learn more about setting file associations and types, refer to the documentation for these two commands in Help And Support Center.



 < Day Day Up > 



Microsoft Windows Command-Line Administrator's Pocket Consultant
MicrosoftВ® WindowsВ® Command-Line Administrators Pocket Consultant
ISBN: 0735620385
EAN: 2147483647
Year: 2004
Pages: 114

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net