Most network administrators have found scripting to be an invaluable tool for automating complex or repetitious tasks. However, although many are familiar with Windows shell scripting and batch files, even the most experienced network administrator may find the powerful VBS scripting somewhat intimidating. VBS scripting is a subset of Microsoft Visual Basic called Visual Basic Scripting Edition. It looks quite different from shell scripting and, in fact, uses a very different concept—one that's based on objects and methods.
But network administrators don’t have to be master programmers to write effective and useful VBS scripts. It’s just a matter of taking a few steps into the unknown. I'm going to introduce you to VBS scripting by discussing the concept of using objects and methods to accomplish a task. We will then apply this concept to work with the Windows file system to carry out a simple file management task.
Objects and methods
Probably the main reason why VBS code appears so strange to someone who is used to writing batch files is that it makes use of a syntax that combines objects and methods to accomplish its tasks. All of the common programming concepts are there—variables and constants, error trapping, conditional processing, and so on. But to get anywhere with VBS, you must start with objects.
The platform for VBS scripting is the Windows Script Host (Wscript.exe). It comes with a set of tools, each of which has specific capabilities. In VBS, a tool is called an object, and the various capabilities of a tool are called methods. So to accomplish a task, you combine an object and a method. The way this is done in VBS syntax is in a dot format like this:
The root object in the Windows Script Host (WSH) is called Wscript. There are 13 other WSH objects, but we don’t need to go into those now. The main thing here is to understand the concept of using objects and methods to carry out a task. The Microsoft MSDN Web site provides more information on the WSH Object Model.
The easiest way to understand the use of objects and methods is to become familiar with FileSystemObject, the object used to interact with the Windows file system. However, because it is not one of the core WSH objects, it can't be used directly. It is actually invoked by WSH from a file called the Scripting Runtime Library (%windir%\system32\scrrun.dll).
Once invoked, FileSystemObject can be used to create, copy, or delete files or folders. You invoke it through a process called “instantiating an object,” which is basically creating the object and then assigning it to a variable.
We will use the Windows Script Host root object Wscript to create the FileSystemObject object (yes, that does sound redundant) and simultaneously assign it to a variable. Here’s how.
One of the methods in the Wscript object is called CreateObject. So using our dot notation, we can write Wscript.CreateObject to accomplish our task. We just need to tell it that we want to create the FileSystemObject found in the Scripting Runtime Library:
However, that's only half the job of instantiating FileSystemObject; we also have to simultaneously assign it to a variable. In programming, a variable is simply a name you give to something so that you (and the program) can keep track of it more easily. Since we are creating the FileSystemObject, we might use FSO as the variable name. But there are many kinds of items besides objects that can be given names. To identify this as the name of an object, we'll adopt Microsoft’s recommended naming convention and use the variable name objFSO.
We are now close to the point where we can actually do something. But in Visual Basic, you can’t use a variable name until you declare it and assign something to it. To declare a variable, we use a separate statement with the keyword Dim, like this:
Then, to assign something to the variable, we use the keyword Set and an equal sign. So the entire process of instantiating the FileSystemObject will look like this:
Set objFSO = Wscript.CreateObject(“Scripting.FileSystemObject”)
Now that we have instantiated FileSystemObject and assigned it to a variable called objFSO, what can we do with it? Remember that objects come with a set of capabilities called methods. We can use those methods to manipulate the file system if we know the exact syntax. Some of the useful methods for FileSystemObject are listed in Table A.
|CopyFile||Copies files to another location|
|FileExists||Returns True or False depending on whether the file exists|
|MoveFile||Moves files to another location|
|CopyFolder||Copies folders to another location|
|FolderExists||Returns True or False depending on whether the folder exists|
|MoveFolder||Moves folders to another location|
Let’s say that we want to write a script that will copy a file named MyFile.txt from a local drive to a network share. Good programming technique dictates that we first test to make sure that the file exists before we execute the Copy command. Listing A shows how we would do that.
Once we instantiated the FileSystemObject as objFSO, we were able to invoke two of its methods (FileExists and CopyFile) to accomplish a task. We also invoked a Wscript method (Echo) to display a message in case of an error. In addition, note that we invoked those methods using the variable name objFSO rather than the object name FileSystemObject.
Of course, the exact syntax is important, such as the If-Then-Else construct and the use of parentheses and quotation marks. But the point here is to understand the use of objects and methods to accomplish a task.
Good programming technique requires you to do one more thing before leaving the VBS script. After using objects you have instantiated, you should always release them before ending the script. You do that by assigning nothing to the variable name you created—literally. Here’s what it looks like:
Set objFSO = Nothing
So now, if we put the whole thing together and include, as we always should, documenting comments, it would look something like Listing B.
Just the beginning
As most network administrators know, you could accomplish this same task with a regular batch file or shell script. If that were all VBS scripting could do, there wouldn’t be much point in taking the trouble to learn it. But VBS scripting can ultimately do a great deal more than shell scripting, including reading and writing to files, reading and writing to the Registry, creating shortcuts, and assigning icons. Once you understand the concept of using objects and methods, you will have taken the first step toward mastering this powerful tool.