tips.paddyonline.net

If you need tips & tricks, here they are...

Using environment variables with Cmd.exe

Using environment variables with Cmd.exe

The Cmd.exe command-shell environment is defined by variables that determine the behavior of the command shell and the operating system. You can define the behavior of the command-shell environment or the entire operating system environment by using two types of environment variables, system and local. System environment variables define the behavior of the global operating system environment. Local environment variables define the behavior of the environment of the current instance of Cmd.exe.

System environment variables are preset in the operating system and available to all Windows XP processes. Only users with administrative privileges can change system variables. These variables are most commonly used in logon scripts.

Local environment variables are only available when the user for whom they were created is logged on to the computer. Local variables set in the HKEY_CURRENT_USER hive are valid only for the current user, but define the behavior of the global operating system environment.

The following list describes the various types of variables in descending order of precedence:

1. Built-in system variables

2. System variables found in the HKEY_LOCAL_MACHINE hive

3. Local variables found in the HKEY_CURRENT_USER hive

4. All environment variables and paths set in the Autoexec.bat file

5. All environment variables and paths set in a logon script (if present)

6. Variables used interactively in a script or batch file

In the command shell, each instance of Cmd.exe inherits the environment of its parent application. Therefore, you can change the variables in the new Cmd.exe environment without affecting the environment of the parent application.

The following table lists the system and local environment variables for Windows XP.

VariableTypeDescription
 %ALLUSERSPROFILE% Local Returns the location of the All Users Profile.
 %APPDATA% Local Returns the location where applications store data by default.
 %CD% Local Returns the current directory string.
 %CMDCMDLINE% Local Returns the exact command line used to start the current Cmd.exe.
 %CMDEXTVERSION% System Returns the version number of the current Command Processor Extensions.
 %COMPUTERNAME% System Returns the name of the computer.
 %COMSPEC% System Returns the exact path to the command shell executable.
 %DATE% System Returns the current date. Uses the same format as the date /t command. Generated by
Cmd.exe. For more information about the date command, see Date
 %ERRORLEVEL% System Returns the error code of the most recently used command. A non zero value usually indicates
an error.
 %HOMEDRIVE% System Returns which local workstation drive letter is connected to the user's home directory. Set based
on the value of the home directory. The user's home directory is specified in Local Users and
Groups.
 %HOMEPATH% System Returns the full path of the user's home directory. Set based on the value of the home
directory. The user's home directory is specified in Local Users and Groups.
 %HOMESHARE% System Returns the network path to the user's shared home directory. Set based on the value of the
home directory. The user's home directory is specified in Local Users and Groups.
 %LOGONSEVER% Local Returns the name of the domain controller that validated the current logon session.
 %NUMBER_OF_PROCESSORS% System Specifies the number of processors installed on the computer.
 %OS% System Returns the operating system name. Windows 2000 displays the operating system as
Windows_NT.
 %PATH% System Specifies the search path for executable files.
 %PATHEXT% System Returns a list of the file extensions that the operating system considers to be executable.
 %PROCESSOR_ARCHITECTURE% System Returns the chip architecture of the processor. Values: x86, IA64.
 %PROCESSOR_IDENTIFIER% System Returns a description of the processor.
 %PROCESSOR_LEVEL% System Returns the model number of the processor installed on the computer.
 %PROCESSOR_REVISION% System Returns the revision number of the processor.
 %PROMPT% Local Returns the command prompt settings for the current interpreter. Generated by Cmd.exe.
 %RANDOM% System Returns a random decimal number between 0 and 32767. Generated by Cmd.exe.
 %SYSTEMDRIVE% System Returns the drive containing the Windows XP root directory (that is, the system root).
 %SYSTEMROOT% System Returns the location of the Windows XP root directory.
 %TEMP% and %TMP% System
and User
Returns the default temporary directories that are used by applications available to users who
are currently logged on. Some applications require TEMP and others require TMP.
 %TIME% System Returns the current time. Uses the same format as the time /t command. Generated by
Cmd.exe. For more information about the time command, see Time
 %USERDOMAIN% Local Returns the name of the domain that contains the user's account.
 %USERNAME% Local Returns the name of the user who is currently logged on.
 %USERPROFILE% Local Returns the location of the profile for the current user.
 %WINDIR% System Returns the location of the operating system directory.

Setting environment variables

Use the set command to create, change, delete, or display environment variables. The set command alters variables in the current shell environment only.

To view a variable, at a command prompt, type:

set VariableName

To add a variable, at a command prompt, type:

set variablename=value

To delete a variable, at a command prompt, type:

set VariableName=

You can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. Consider the following examples: • To create the variable value new&name, type:

set varname=new^&name • To create the variable value "new&name", type:

set varname="new&name" • If you type set varname=new&name at the command prompt, an error message similar to the following appears:

"'name' is not recognized as an internal or external command, operable program or batch file."

Variable names are not case-sensitive. However, set displays the variable exactly as you typed it. You can combine uppercase and lowercase letters in your variable names to make your code more readable (for example, UserName).


Note • The maximum individual environment variable size is 8192bytes. • The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB. Top of pageTop of page Substituting environment variable values

To enable the substitution of variable values at the command line or in scripts, enclose the variable name in percent signs (that is, %variablename%). By using percent signs, you ensure that Cmd.exe references the variable values instead of making a literal comparison. After you define variable values for a variable name, enclose the variable name in percent signs. Cmd.exe searches for all instances of the variable name and replaces it with the defined variable value. For example, if you create a script that contains different values (for example, user names) and you want to define the USERNAME environment variable for each user with these values, you can write one script using the variable USERNAME enclosed in percent signs. When you run this script, Cmd.exe replaces %USERNAME% with the variable values, which eliminates the need to perform this task manually for each user. Variable substitution is not recursive. Cmd.exe checks variables once. For more information about variable substitution, see For and Call

Source: Here!

Add comment