Prepare yourself for a crash course in NT command shell scripting, where you’ll learn all about variable declaration and substitution and the FOR command’s powerful file parsing mode. Invest your time here and you’ll be able to create scripts that use the FOR command to perform repetitive administrative tasks, such as creating directories and network shares and stopping NT services.
Variable declaration and substitution
Before we show you how to use the powerful FOR command, we must briefly explain the concepts of variable declaration and substitution. Variables are indexes for memory counters that hold a specific value. NT command shell variables can contain either alphabetic or numeric values. To display the NT command shell’s preset environment variables, type the command SET. To declare a new variable and set its value, type:
Variable is the variable’s name and value is the variable’s value. 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. For example, if you execute these three command statements:
SET /a count=1
SET /a count=%count% + 1
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.
What is FOR?
The FOR command is the NT command shell’s iterator; it iterates or repeatedly executes other commands. You can use the FOR command to perform remarkable feats, such as creating a directory on every one of your NT servers by executing a single command at the command prompt or in a script. Without FOR, NT command shell scripts would be incredibly cumbersome and not nearly as useful—you would have to issue a separate command for each server to create a directory on each of them.
Because FOR is so powerful, it’s no surprise that its syntax is the most sophisticated of all of the shell commands. Type FOR /? > FOR.TXT to capture the FOR command’s lengthy help screens to a text file you can print and keep handy for reference.
We won’t attempt to show you every possible use of the FOR command. Instead, we’ll show you how to use the FOR command’s text parsing capability to perform some administrative tasks. To use FOR to parse text, use the syntax:
FOR /F “tokens=T delims=D eol=E” %I in (Input File) do Command
The /F switch puts FOR in text parsing mode. T is the token(s) that you want to read from each line of text. D is the delimiter used to separate tokens. E is the character used to mark the end of a line of text. The Input File is the name of the file that FOR parses, and Command is the command that FOR iterates. Seem complicated? You’ll be typing FOR commands blindfolded before you know it, but for now you can learn best by example.
FOR performs repetitive tasks, so you don’t have to
Suppose that you want to create a directory on each server in your organization. But you also want to create the directory on different drives, depending upon which server you’re working with. You can use a single FOR command to accomplish this task, but first you must prepare your input file. To do so, create a file called Servers.txt. Put each server’s name on separate line, followed by a comma and the drive where you want to create the new directory. If you want FOR to skip a line in the input file, put a semicolon at the beginning of the line. Figure A shows an example of what Servers.txt should look like.
|A sample input file for use with FOR /F|
Log on to a workstation with an account that has the permissions necessary to create the shares, open the NT command shell, and issue the command:
FOR /F “tokens=1,2 delims=, eol=;” %I in (SERVERS.TXT) DO MD \\%I\$%J\Dirname
where Dirname is the name of the directory you want to create. This FOR command parses the first line of text in Servers.txt, looking for two tokens (1 and 2) separated by a comma. A token is string of text that FOR will set as a variable’s value. This statement sets the variable I to the value of the token 1 and sets the variable J to the value of token 2. This statement then interprets and executes the command to the right of the DO keyword, but first substitutes the values of the I (token 1) and J (token 2) variables. After executing the command, FOR will then reiterate and parse the second line of text in Servers.txt, and so on, until it reaches the end of the file. Therefore, if you specified CMD as the directory name, the single FOR command would in turn execute each of the following commands:
creating the required shares on each server. FOR skipped the line that began with the EOL marker (a semicolon).
Although you could have typed and executed each of these commands individually, imagine how efficient this command would be if you needed to create directories on more than 50 servers. Now consider how useful it would be if you needed to share the CMD directories you created. You can create a FOR statement that uses the NET SHARE command to share the directories. This FOR command can use the same Servers.txt file. Just type:
FOR /F “tokens=1,2 delims=,” %%I in (SERVERS.TXT) DO NET SHARE CMD=\\%%I\$%%J\CMD
You can even use FOR with Windows NT Resource Kit command line utilities. The Kit’s NETSVC tool lets you remotely administrate NT services. If you wanted to stop the Message Transfer Agent Service on all your Microsoft Exchange Servers, you could use the statement:
FOR /F “tokens=1 delims=,” %%I in (SERVERS.TXT) DO NETSVC MSExchangeMTA \\%I /STOP
As you can see, if you spend the time and keystrokes to create a Servers.txt file, FOR will spare you from tediously entering repetitive commands—or worse yet, from using NT’s GUI.
Please Note: When you use FOR in a script file, you must place double percent signs before FOR iterator variables instead of the single percent signs we used above. Therefore, the first FOR statement we showed you would appear in a script file as:FOR /F “tokens=1,2 delims=,” %%I in (SERVERS.TXT) DO MD \\%%I\$%%J\DirnameThe authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.