With Windows shell scripting and VBS, you can map network drives, copy files or folders, edit the registry, create shortcuts, and perform a host of other useful network administrative tasks. In my previous articles on scripting concepts, I alluded to some programming constructs that let you extend the functionality of your scripts. Now it’s time to take a look at these programming constructs, see how they are used, and examine their syntax in both shell scripting and VBS. The constructs I’ll discuss are conditional processing, error trapping, and iterative processing.


Allen Rouse’s series on admin scripting


Conditional processing in Windows shell scripts
Conditional processing is the ability to have your script test whether a certain condition exists or is true, such as determining whether a variable is equal to a string or a specific file is present. Once the script determines whether the condition is true or false, it can carry out one command if the condition does exist and carry out another command if it does not exist.

In shell scripting, the conditional processing construct consists primarily of these elements:

  • IF statement
  • GOTO statement
  • Labels

The IF statement is used to test a condition. This statement may include a command to be carried out if the condition is true, such as:
 
IF %SYSTEMROOT% == “C:\WINNT” COPY new.dll C:\WINNT\SYSTEM32
 

You can also include a command to be carried out if the condition is false. To do this, you add an ELSE clause to the IF statement:
 
IF %USERNAME% == “Johnny” echo “Hello Johnny” ELSE echo “Who are you?”
 

In addition, you have the option of branching to another part of the script to carry out a command. This is especially useful in more complex processing. To branch, you must include a label, which is basically a named place marker in your script. For example, if your script contains commands that can’t be carried out on a Windows 9x operating system, you might want to test to see that the operating system is Windows NT or higher first. If it is not, you do not want to the script to run at all. To accomplish this, you could use a label called EXIT and the GOTO statement:
 
IF NOT %OS% == “WINDOWS_NT” GOTO EXIT
[Body of script here]
GOTO EOF
:EXIT
ECHO “You must be using Windows NT or higher.”
 

A little explanation is called for here. First, note that a colon must precede the label (EXIT) in the script and that I used two equal signs (==) instead of one (=). The double equal sign is called a comparison operator and must be used with IF statements. I used a GOTO statement to tell the script to branch to the label. Remember that the script will carry out each line in order until it reaches the end, unless it is told otherwise. This means that if the condition is true, it will still carry out that last ECHO statement in the script. Of course, I don’t want to echo that message if the condition is true, so I used the GOTO EOF (End of File) statement before the EXIT label. This tells the script to end there and not carry out any more commands—in this case: ECHO“You must be using Windows NT or higher.”

You can always use NOT to invert any test for a condition. Thus, in addition to the basic IF statement, you can use the following:
 
IF (NOT) DEFINED [Tests to determine whether a given variable exists]
IF (NOT) EXIST [Tests to determine whether a given file or folder exists]
 

The Command Reference in Windows Help provides details and notes on using these statements.

Dealing with errors in Windows shell scripts
Every time a Windows shell command completes its task, it exits with a specific code. Commands that are completed with no errors exit with the code of 0, while any other exit code indicates that an error occurred. For instance, say that you execute the following command:
 
DEL myfile.txt
 

If the file was successfully deleted, the exit code will be 0. But if that file doesn’t exist, or if it has a read-only attribute, the command can’t delete it and the error code will be something other than 0. A variable called ERRORLEVEL lets you access that code, and you can use that in conjunction with the IF statement to trap errors. Here’s an example:
 
DEL myfile.txt
IF NOT ERRORLEVEL 0 GOTO EXIT
ECHO “File deleted.”
GOTO EOF
:EXIT
ECHO “Could not delete file.”

Conditional processing in VBS
In VBS, the syntax for conditional processing is a little different. To execute a straightforward IF statement, you type something like this:
 
IF condition THEN
     one or more statements
END IF
 

Notice that you must include the keyword THEN and explicitly end the IF statement. In addition, VBS will allow you to include statements that will be executed if the condition is false, as well as statements to execute if the condition is true. This is done with the ELSE clause, as shown here:
 
IF count = 10 THEN
     Wscript.echo “Count is ten.”
ELSE
     Wscript.echo “Count is not ten.”
END IF
 

VBS also has a slightly more efficient method of performing conditional processing, known as the SELECT CASE statement. It allows the script to evaluate a condition, as you might guess, on a case-by-case basis. Here’s an example:
 
SELECT CASE count
    CASE count = 1
         Wscript.echo “Count is one.”
    CASE count = 2
         Wscript.echo “Count is two.”
    CASE count = 3
         Wscript.echo “Count is three.”
END SELECT

Iterative processing in shell scripting
Iterative processing, also called looping, means repeating one or more commands until some goal is reached. In shell scripting, the syntax is not as obvious as other constructs and is therefore a little harder to learn. For iterative processing you use the FOR DO statement, and the syntax looks like this:
 
FOR /switch %%variable in (set) DO command
 

The /switch element can be one of four switches:

  • /l loops through a range of values.
  • /f loops through or parses a string.
  • /d loops through the files of a folder.
  • /r loops through all subfolders.

The %%variable is any letter of the alphabet, such as %%a or %%z. The FOR command will increment the value of this variable each time it passes through the loop.

Look at how you might use this construct for a network admin task. Suppose that you need to print a list of all the subfolders in the C:\WINNT folder. You could do that using a straight shell command such as:
 
DIR C:\WINNT /AD /S
 

The output would look similar to Figure A.

Figure A

However, this may not be the most useful format. So l create a script with the following commands:
 
@echo off
FOR /r c:\winnt %%a in (.) do echo %%a > dirlist.txt
 

The resulting output, redirected to a text file, will appear similar in format to Figure B.

Figure B

Iterative processing in VBS
Iterative processing in VBS is frankly more useful and, as an added bonus, the syntax is actually easier. Four forms of looping are available in VBS:

  • DO WHILE will create a loop that continues to process as long as a condition is true.
  • DO UNTIL will create a loop that continues to process until a condition becomes true.
  • FOR NEXT will create a loop that executes a specific number of times.
  • WHILE WEND will create a loop that continues to process as long as a condition is true.

The WHILE…WEND loop is similar to DO…WHILE, but it is not used much, due to the general popularity of DO…WHILE. The basic formats for the other three constructs are:
DO WHILE condition
    Repeating commands
LOOP
—————————————————
DO UNTIL condition
    Repeating commands
LOOP
—————————————————
FOR a = 1 TO 10
    Repeating commands
NEXT
—————————————————

It’s important to understand that in both the DO…WHILE loop and the DO…UNTIL loop, the condition must change at some point. Otherwise the loop will never end. The FOR…NEXT loop shown above will repeat 10 times.

Be constructive
Using the programming constructs I have discussed here, you can greatly improve the efficiency and functionality of your scripts. You can have your scripts make decisions without your intervention, recognize errors and take action based on them, and quickly perform repetitive tasks. As with all elements of scripting, developing the skills to use these constructs will take practice, but it will be well worth the trouble. In a future article, I’ll provide details on other resources you can use to learn about these constructs.