Setting Environment Variables


Settings that you're not likely to use but could still prove handy are the environment variables. What do environment variables do? They're a way to communicate setup information to application programs, from the days before the Registry. They indicate where temporary files are stored, what folder contains Windows program files, and other settings that affect program operation and system performance. In particular, batch files and application programs can use them to find where certain system files and folders are located.

Windows sets up a set of environment variables for you, including the ones listed in Table 25.1.

Table 25.1. Some Windows XP Environment Variables

Variable Name

Contains

ALLUSERSPROFILE

The path to the folder that contains files visible to all users, including desktop items, start menu items, and the "Shared Documents" folder[*]

COMPUTERNAME

The network name of this computer[*]

OS

"Windows_NT", provided to let you write batch files that know when they're running on Windows NT, 2000, or XP, as opposed to MS-DOS, Window 3.1, or Windows 9x[*]

PATH

The list of folders that Windows searches when you try to run a program by entering only its name

PATHEXT

The list of file extensions that Windows considers to indicate runnable programs: .COM, .EXE, .BAT, and so on.

PROMPT

A specification of how Windows is to display the prompt in a Command Prompt window. By default, it is $P$G, which displays the current drive and folder behind the >

TEMP

The folder in which applications are to create temporary (scratch) files

TMP

Like TEMP. (Some applications look at the TMP variable and other applications look at TEMP to get the name of the temporary file folder)

USERNAME

The logon name of the current user[*]

USERPROFILE

The path to your profile folder, the folder that contains My Documents, your Start Menu, and so on[*]

SystemRoot

The path to the system32 folder in the Windows installation folder[*]

windir

The path to the Windows installation folder[*]


[*] These variables are informational only; changing their value doesn't change the corresponding location or name.

In DOS and Windows 9x, environment variables were usually set up in the AUTOEXEC.BAT file, using lines like this:

 SET PROMPT=$P$G SET TEMP=C:\TEMP SET PATH=C:\WINDOWS;C:\DOS;C:\MOUSE;C:\BIN 

In Windows XP, environment variables for Windows applications are set up using a nifty graphical user interface. In addition, old DOS and Windows 3.x applications will see any environment variables set up by the AUTOEXEC.NT file, which we'll discuss later in this chapter, under "Configuring the MS-DOS Environment."

NOTE

The environment variables defined here are seen by every Windows application, and are the initial environment variables set for each command prompt window. If you change environment variables in the command prompt window itself (say, using the set command in a batch file), the changes apply only to that window and will disappear when the window is closed.


By clicking the Environment Variables button on the System applet's Advanced tab, you can change the environment variables in the resulting dialog box (see Figure 25.9).

Figure 25.9. Examining the Environment Variables for the current user (top) and for all users of the system (bottom).


In this dialog box, you can create new variables, delete a variable, or edit a variable using the corresponding buttons. Notice that this dialog box has two sections, System Variables and User Variables. Environment variables set in the System Variables area are the default settings provided for every user account. This section can only be edited by while logged on to a Computer Administrator account. The User Variables section applies just to the currently logged-on user (that is, you), and each user can edit his or her own settings.

If you need to alter a variable, you must understand what happens if there's a conflict between environment variables defined in more than one place. As a rule, Windows examines several locations for definitions, and the last definition seen wins. Windows XP sets variables in the following order:

1.

System-level variables.

2.

User-level variables. (However, at this step, the PATH variable is treated specially. See the next section for details.)

3.

AUTOEXEC.NT-declared variables (seen only by MS-DOS or Windows 3.x applications). AUTOEXEC.NT contains commands similar to those used in the AUTOEXEC.BAT file used by older versions of Windows. We'll discuss AUTOEXEC.NT later in this chapter.

4.

Subsequent definitions, issued by set commands typed at the command prompt or encountered in a batch file. Environment changes made in a batch file persist at the command prompt, unless the batch file uses the SETLOCAL command.

CAUTION

I need to make an important distinction here. AUTOEXEC.NT settings (such as the SET BLASTER command, which sets up sound support) do not affect the default variables seen in the standard Command Prompt window, which is actually a 32-bit environment running the 32-bit CMD.EXE "shell" program. The AUTOEXEC.NT variable settings go into effect only when the Execution of an MS-DOS or Windows 3.x program causes a Virtual DOS Machine (VDM) to be created, and are seen only by the program(s) running inside the VDM. The Virtual DOS Machine subsystem is described in more detail later in this chapter.

You can view the effective MS-DOS environment by opening a Command Prompt window, running the old MS-DOS shell program COMMAND.COM, and typing set to list the defined variables.


Why modify system variables? Good question. Not many people need to. Probably the most likely reasons to modify these variables are to change the folder that applications use to create temporary work files, and to add directories to the system's search path. Or, you might have particular applications that require special environment variables set. Finally, some command-line utilities look for environment variables to set the default value of certain options. For example, copy checks to see whether an environment variable named COPYCMD has been defined, and if it has, it interprets COPYCMD as containing command line switches that can be used, for instance, to determine whether or not copy should prompt before overwriting files. I'll discuss these options in the next several sections.

Specifying the Location of Temporary Files

Many applications create temporary files to hold information while you're working. These files are usually deleted when the application exits. Sometimes, however, they're not deleted, and they can accumulate, taking up a lot of disk space. To make management simpler, you may wish to control where they are stored. By custom, most applications that do create temporary files create them in the folder named by either the TMP or TEMP environment variable.

By default, both TMP and TEMP are defined in the System Settings list as %userprofile%\Local Settings\Temp. %userprofile% is an environment variable that contains the full path to your user profile folder. The net result is that, for my account, temporary files are created in C:\Documents and Settings\bknittel\Local Settings\Temp.

Now, for maximum security, placing each user's temporary files in a different folder is a good idea. Also, with Fast User Switching active, several users could have applications running at the same time, so placing temporary files in a different folder for each user avoids the possibility of conflicts in the filenames. On my own computer, however, I am not worried about inter-user security, and to make cleanup easier, I personally prefer to place temporary files for all users in a folder named \temp on the hard drive with the most space. On my computer this is D:\temp.

To specify the location of temporary files for your account alone, set the values of variables TEMP and TMP in the User Settings section. To specify the same location for all accounts, set the values in the System Settings section and delete any settings in the User Settings section.

NOTE

If you designate a folder in a partition that is formatted with the NTFS file system, be sure that the user or users who are set up to use it have read and write permissions on the folder! Ensure this by editing the folder's security properties, adding the group Users, and giving Users all permissions except Full Control. You may need to temporarily disable Simple File Sharing to view the Security properties.


NOTE

You might be inspired to create a separate folder for each user with a setting like D:\temp\%username%. This will work, but you must create the folder for each user in advance. Windows will not create the folders automatically, and if the folder specified by TEMP or TMP does not exist, most applications will either fail, or will create temporary files in seemingly random locations.


Setting the PATH Environment Variable

Another important environment variable is called PATH. This variable lists the folders that Windows is to search whenever you attempt to run a program by typing its name without a path specification. For example, if I type regedit into a Command Prompt window, or into the Start, Run dialog, the Registry Editor window appears. But how does Windows know that "regedit" meant C:\windows\system32\regedit.EXE? The answer is the PATH variable. On my computer, PATH contains this:

[View full width]

D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS; C:\WINDOWS\system32\WBEM;"C:\program files \scripts"; "C:\Program Files\Symantec\pcAnywhere";c:\progra~1\winzip;"c:\Documents and Settings\bknittel\scripts";c:\bin;c:\bat

The value of the PATH variable is a list of folder names separated by semicolons. If a folder name has a space in it, it is enclosed in double quotes, as "C:\program files\scripts" and "C:\Program Files\Symantec\pcAnywhere" are on my computer.

Now, when I type regedit as a command name, Windows searches the folders named in the PATH variable in order, looking for the first one that contains an EXEcutable program whose name is regedit. It finds the standard Windows utility in the second folder, c:\WINDOWS\system32.

NOTE

The PATH variable is used for Windows programs as well as DOS programs; it's used any time a program is not specifically specified with a full path.


Many applications add their own installation folders to the path during installation. Thus, you will seldom if ever need to modify the PATH to provide access to programs that are designed to run from the command line. If you write batch files or scripts, however, it's very useful to put all of these into one folder and to enter this folder name into the PATH, so that you can run your batch files and scripts simply by typing their name.

Because it's so common for users to want to put a personal folder into the PATH, and because mis-editing the PATH variable can prevent Windows from being able to find applications it needs to run, Windows gives the "User Variables" PATH definition special treatment.

  • For the PATH variable, the User Variables definition is appended to the System Variable definition.

  • For all other environment variables, a User Variables definition overrides a System Variables definition.

In other words, you can enter your own personal folder(s) into the User Variables definition of PATH without worrying about messing up the standard definitions.

For example, a few paragraphs back I showed my computer's PATH setting. It's set up this way: the System Variables PATH definition is

 D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM; "C:\program files\scripts";"C:\Program Files\Symantec\pcAnywhere"; c:\progra~1\winzip;"%USERPROFILE%\scripts" 

These folders are used for every user on my computer. The User Variables PATH definition contains only my personal folders:

 c:\bin;c:\bat 

When I log on, Windows automatically adds my personal folders after the system-wide folders.

TIP

To make it even easier to have personal PATH folders, you can automatically give each user a place to store their own personal programs by placing %USERPROFILE%\scripts in the System Variables PATH definition. This gives each user the option of creating a folder named scripts in their profile folder, into which they can put commonly used batch files, programs, and scripts.


You can also modify the PATH variable by directly setting the environment variable at the command prompt. It's best to do this by adding a new folder at the beginning or end of the existing path list, rather than by replacing the list entirely. Type the string %path% where you want the original PATH contents to appear. For example, you can add the folder c:\myfolder to the head of the path list by typing

 set path=c:\myfolder;%path% 

or to the end of the path by typing

 set path=%path%;c:\myfolder 

You also can use this technique to modify the path from within a batch file.



Special Edition Using Microsoft Windows XP Professional
Special Edition Using Microsoft Windows XP Professional (3rd Edition)
ISBN: 0789732807
EAN: 2147483647
Year: 2003
Pages: 450

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