Microsoft

Tech Tip: Learn the basics of command shell variables

Variables are indexes for memory counters that hold a specific value. To display the Windows NT command shell's preset environment variables, type the command SET. To declare a new variable and set its value, type the following:

SET variable=value

Replace variable with the variable's name, and replace value with the variable's value.

Command shell variables in NT can contain either alphabetic or numeric values. The NT command shell treats all variables you set with the SET command as text unless you use SET with the /a parameter to type the variable as numeric.

Once you've set the variable, you can substitute its value in a command by enclosing the variable's name in percent signs.

SET /a count=1
SET /a count=%count% + 1
Echo %count%

The last ECHO command statement will output the number 2 on the screen. The most important thing to remember about variable substitution is that the NT command shell substitutes variables before it interprets any part of the command. Therefore, you can use variables to construct any part of a command statement, including a command itself.

Redirect the command shell's standard output

NT command shell commands and most command console applications, such as those found in the Windows NT Server Resource Kit, output ANSI character data that the command shell displays as text on your screen. This text output is also known as standard output, and you can capture it to files, pipe it as standard input to other commands, or assign its value to a variable.

To capture output to a file, use the redirection symbols [>] or [>>]. The single greater than symbol captures the output to the specified file while overwriting the existing file's contents. The double greater than symbol captures the output and appends the data to the target file's contents. For example:

DIR > dir.txt

This captures the DIR command's output to the text file Dir.txt and overwrites any preexisting data that the file contained.

Editor's Picks

Free Newsletters, In your Inbox