Windows Script Host tutorial: Pseudocode algorithms and other programming basics

Want to script your own Windows solutions? In this Drill Down by Greg Schultz, you'll learn some programming techniques that will help you prepare to script in VBScript or JScript.

In several of my Daily Drill Downs, I showed you how to take advantage of the Windows Script Host and VBScript to automate command-line operations inside the Windows GUI. After reading those articles, you may be wondering how to create solutions of your own. In this Daily Drill Down, I’ll show you how to prepare a script idea for programming. I’ll also guide you though the process of selecting and studying scripting languages.
If you’re interested in Windows scripting, you’ll want to read these features:
Getting started
Your scripting will be more successful if you follow a series of tried-and-true steps that programmers use. These steps begin with stating your ideas in a series of outlines before starting your script. It’s true that this process is a bit more involved than jumping into a program, but it ultimately results in more success—especially if you’re at the beginner or intermediate level.

If you haven’t yet read the Daily Drill Downs listed above, you’ll want to do so as I’ll use scripts discussed there as examples. You’ll also find that those scripts will provide you with good examples for designing your own scripts. “Getting started with Windows Script Host” makes a good prerequisite for this tutorial series as it introduces the Windows Script Host object model, which is at the core of the scripting languages.

Do you have what it takes?
While it helps to have a programming background, it's not necessary. It’s also not necessary to use third-party tools. Since Windows Script Host and its native scripting engines, VBScript and JScript, are integrated into the Windows operating system, and since all you need to create scripts is Notepad, you already have all the tools you need to get started. If you’re a beginner or intermediate programmer, keep in mind that while your learning curve will be steeper, with patience and determination, you can succeed.

Creating an idea description and outline
Once you have an idea for a Windows task you want to automate, the first thing you should do is create a description. Next, create an outline. These two steps serve as starting points from which you can build and refine your idea into a script. All you need to do is compose a simple paragraph describing what it is you want your script to accomplish. Then, break down the steps that you think the procedure will involve. For example, here’s my idea outline for the Ping Automation Tool script:
The general goal of this script is to allow the user to run the ping command from within the Windows GUI without having to shell out to a command prompt and manually type the command. More specifically, the goal of this script is to automate the process of pinging a range of Class C IP addresses.
  1. The script will prompt the user to enter a range of IP addresses.
  2. It will run the ping command with each of those addresses and compile a report of the results.
  3. It will display the report in an easy-to-read format.

As you can see, the description section lays out the goals for the script in just two sentences. The outline then presents the basic steps that the script will perform. This simple information will go a long way when it comes to the next step in the process: creating a pseudocode algorithm.

Creating a pseudocode algorithm
Once you have an idea description and a rough outline, the next task is to develop a pseudocode algorithm. Think of a pseudocode algorithm as a more detailed outline that spells out each step the script will perform. This algorithm is very important to the development process because it will form the basis of your script.

Pseudocode is a set of instructions written in plain English that mimics the code you’ll use to write your script. By writing pseudocode, you can focus on the structure of your script without having to worry about the idiosyncrasies of a specific scripting language. In addition, if you’re very meticulous, you can work out most of the kinks in your programming logic. The more kinks you work out, the easier it will be to translate your pseudocode algorithm into the scripting language you choose.

This is the pseudocode algorithm I developed for my Ping Automation Tool:

1. Since the first three octets of the range of IP addresses will be the same, prompt the user to enter them first.
2. Prompt the user to enter the first and last octets in the range of IP addresses to be tested.
3. Create a text file to hold the results of the ping commands.
4. Since the tool can be run multiple times and use the same text file to hold the cumulative results, check to see if the file already exists.
         a. If the file does exist, append information to the existing file.
         b. If the file doesn’t already exist, create a new file.
5. Create a header in the file that contains the range of IP addresses being tested, as well as the time and date of the test.
6. For each IP address in the range:
         a. Shell out to a command prompt.
         b. Run the ping command.
         c. Add the results to the file.
7. Launch Notepad and open the file to display the results of the ping commands.

If you compare this pseudocode algorithm to the Ping Automation Tool script presented in “Automate ping to test a range of IP addresses with this VBScript program,” you’ll see that the pseudocode does a decent job of outlining the script. However, changes were required during the scripting stage.

For example, I discovered that in addition to the first three octets, it was important to ensure that the user also typed in the last period in the IP address. Furthermore, due to the fact that the VBScript InputBox function can only retrieve one piece of information at a time, step two in the pseudocode algorithm ended up as two separate steps with two InputBox function commands in the script.

This comparison shows that you won’t be able to anticipate everything in your pseudocode. When you write your script, you may encounter new ideas or problems. Furthermore, the scripting language may provide you with tools or features that will allow you to expand your script. Most likely, the scripting language will impose restrictions as well as add steps to your original pseudocode.

Selecting the right scripting language
Once you’ve completed your pseudocode algorithm, you’re ready to begin investigating a scripting language. As I’ve mentioned before, Windows Script Host comes with built-in support for VBScript and JScript, which are based on the Visual Basic and Java programming languages, respectively. In addition, you can add other scripting languages to the Windows Script Host with third-party add-ons. For example, you could add scripting engines for the Perl or Python scripting languages.

Unless you’re very familiar with one of the nonnative scripting languages, I suggest that you stick with either VBScript or JScript. Both VBScript and JScript provide you with similar functions; choosing between them is more a matter of personal preference. If you have experience with Java or JavaScript, then you’ll feel right at home with JScript. If you have experience with Visual Basic or Visual Basic for Applications, then you’ll feel right at home with VBScript.

If you’re new to scripting, create your first script with VBScript. VBScript has a simple syntax that makes it ideal for beginners and intermediates.
Spend some time studying scripting languages to learn the features they offer. Visit the Microsoft Windows Script Technologies site. You’ll find separate documentation pages for VBScript and JScript. While you can study the documentation online, I recommend that you download the documentation for easy reference.
You’ll also want to become familiar with Windows Script Host, as both scripting languages rely on its objects and resources. Microsoft’s Script Technologies site also provides Windows Script Host documentation.
Both the VBScript and JScript scripting language Help files begin with a User’s Guide, which provides an excellent introduction. After you’ve studied the User’s Guide, read the Language Reference section. As you do, pay particular attention to the core components of the scripting language. These core components are the language’s variables, constants, operators, statements, functions, objects, methods, and properties.
Core components
Let’s take a closer look at the core components of a scripting language. You can find examples of how to use these core components in my Daily Drill Downs.

Variables are used by scripts to store information that the script will use as it does its job. For example, the Rename Files script uses the InputBox function to prompt the user for information that it then stores in a variable called OldString.
OldString = InputBox("Enter the characters in the
filename that you want to replace","Rename Files")

Constants are used like variables but have a fixed value. They’re often used to make the code more readable. For example, the Ping Automation Tool script uses constants to assign text to special codes that are used when working with text files.
Const ForWriting = 2, ForAppending = 8
Set TheFile = FileSystem.OpenTextFile
("c:\Pingfile.txt", ForAppending, True)

Operators allow you to perform mathematical and Boolean operations as well as a few specialty operations, such as concatenation, in which two or more items are combined into one. For example, the IpcAutoMain script from the IPConfig Automation Tool uses the concatenation operator (&) to combine the contents of two variables on one line in the report header:
OutputFile.WriteLine("      " & Choice & " View ")

Statements are complex tools that perform some operation in the script. Typically, statements make up the meat of a script, and most scripts will use a combination of statements to perform their task. For example, the Rename Files script uses an If…Then…Else statement to determine the outcome of the operation and display an appropriate operation.
If Success = 1 Then
  Dummy = WshShell.Popup ("Rename Files
operation complete!",5,"Rename Files",64)
  Dummy = WshShell.Popup ("Rename Files operation failed!
   Please repeat the operation.",0,"Rename Files",16)
End If

Functions are self-contained tools that are designed to perform one specific operation and return a result. The scripting languages have a set of built-in functions that you can use from within your script. For example, the Rename Files script uses the built-in Replace function to replace characters in a filename.
ThisFile = Replace(ThisFile, OldString, NewString)

Objects are at the core of the scripting languages and allow your scripts to access and manipulate the Windows environment. Objects are basically components that provide you with a collection of subroutines (methods) and variables (properties) for working with that object from within a scripting language. For example, the IpcAutoMain script from the IPConfig Automation Tool uses the FileSystemObject to get access to a text file containing data that it needs to do its task.
Set FileSystem = CreateObject("Scripting.FileSystemObject")

Methods are programs built in to objects; you’ll use methods to perform certain operations. For example, the IpcAutoMain script from the IPConfig Automation Tool uses the OpenTextFile method as a program that opens the text file containing the data that it needs:
Set InputFile = FileSystem.OpenTextFile
("c:\IpcAuto\IpcAutoChoice.txt", ForReading, True)

Properties are attributes that are used to describe an object’s state. For example, the Rename Files script accesses the Path property to get the full path to the folder that the user selected in the Browse For Folder dialog box.
FullPath = SelectedFolder.ParentFolder.ParseName(SelectedFolder.Title).Path
As you study the User’s Guide and the Language Reference, keep in mind that the goal isn’t to memorize all this information. Rather, the goal is to become familiar with the features and tools that the scripting language makes available, so that when you create your script, you’ll have an idea of what you have to work with.
Creating your script
When you’re ready to begin translating your pseudocode algorithm into a scripting language, load Notepad and begin typing it in. As you do, you’ll want to refer to the scripting-language documentation for details on implementing the commands that you want to use. You’ll also find it beneficial to look at other people’s scripts for references on how to structure yours. In fact, you may find it useful to copy and paste portions of other scripts into yours and then customize them to suit your needs (provided they aren’t protected by copyright, of course).

More to come
At first, creating scripts for the Windows Script Host may seem daunting, but by following the tried-and-true steps of studying the basics of a scripting language, creating descriptions, outlines, and pseudocode algorithms, and then creating your code, anyone with any level of computing experience can be successful. In my next tutorial, I’ll show you some tools you can use to enhance your scripting experience.
At the end of “Getting started with Windows Script Host” you’ll find a list of newsgroups, Web sites, and books. Speaking of books, you’ll be happy to know that your TechProGuild membership enables you to access TechProGuild's Online IT Library, where you can find "Sams Teach Yourself Windows Script Host In 21 Days."The 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.


Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks