Getting Acquainted with Variables

 < Day Day Up > 



In command-line scripting, what we commonly call variables are more properly called environment variables. Environment variables can come from many sources. Some variables are built into the operating system or derived from the system hardware during startup. These variables, called built-in system variables, are available to all Windows processes regardless of whether anyone is logged on interactively. System variables can also come from the Windows Registry. Other variables are set during logon and are called built-in user variables. The built-in user variables available are the same, no matter who is logged on to the computer. As you might expect, they are valid only during an actual logon session, that is, when a user is logged on.

You can see a listing of all the variables known in the current instance of the command shell by typing set at the prompt. In addition to the normal system and user variables, you can create variables whenever Windows is running, which is exactly what you’ll do when you program in the command shell. You define variables for the current instance of the command shell using the SET command and the following syntax:

set variable_name=variable_value

such as

set working=C:\Work\Data
set value=5
set string="Hello World"

Some variables, including system and user environment variables, have special meaning in the command shell. These variables include path, computername, homedrive, and many other important environment variables. One environment variable that you should learn more about is errorlevel, which tracks the exit code of the most recently used command. If the command executes normally, the error level is zero (0). If an error occurs while executing the command, the error level is set to an appropriate nonzero value. Error values include

  • 1 Indicates a general error

  • 2 Indicates an execution error, meaning the command failed to execute properly

  • 2 Indicates a math error, such as when you create a number that is too large for the command shell to handle

You can work with the errorlevel variable in several ways. You can check for a specific error condition, such as

if "%ERRORLEVEL%"=="2" echo "An error occurred!"

Or, you can use the following special syntax and check for a condition equal to or greater than the specified exit code:

if errorlevel 2 echo "An error occurred!"
Note

You’ll see more on errorlevel and if statements later in the chapter in the section titled “Using Conditional Statements.”

When you are finished working with variables, it’s good form to dispose of them. You do this to free memory used by the variable and prevent problems or unexpected results if you accidentally refer to the variable in the future. To clear out a variable, you simply set the variable equal to nothing, such as

set working=

Now the variable is cleared out of memory and is no longer available.



 < 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