If you're familiar with all aspects of the VBScript language, you're ready to start incorporating them into your scripts. To help you, I'll provide an example of how you can use VBScript arrays in a script and discuss a potential pitfall if you want to use both VBScript and JScript arrays in a script.
Suppose you want to create a script that automates server connections. You have a text file that contains a list of servers that you want to connect to one another. In this file, each line contains a server name followed by a carriage-return and linefeed (CR+LF) combination. The servers that you want to include will likely change in the future, so you decide to create a dynamic array. You want each index in this array to contain a server name.
Listing 1 contains a script that reads the server names into a dynamic array called arrText. As Listing 1 shows, you use FileSystemObject (FSO) and TextStream (TS) objects to manipulate the text file. FSO and TS objects are part of the Scripting object model that both VBScript and JScript follow. These objects let you use VBScript code to work with OS files and folders. (I'll cover FSO and TS objects in more detail in a future installment of the "Understanding VBScript series." You can also read about them in Alistair G. Lowe-Norris, "An ADSI Primer, Part 6: Using ADSI to Create and Manipulate User Accounts," June 1999, and Michael Otey, "Using WSH for Windows Automation," December 1998.)
After opening the file, you read the file line by line and add each server name to the end of arrText. However, before you can add indexes to the array, you need to resize the array with ReDim. Notice that you declared arrText as an uninitialized array, which means you can't use the UBound function to get the array's upper bound because no upper bound exists at the moment. Hence, if you use code such as
you'll get an error. For that reason, you must use the iUpperBound variable to resize the array.
As Listing 2 shows, you can use the Split function to make the script in Listing 1 more efficient and understandable. The delimiter is the vbCrLf constant, which evaluates to the CR+LF combination.
Windows Scripting Host (WSH) lets you use VBScript and JScript code. However, if you use both VBScript and JScript in a script and that script uses arrays, you might encounter problems because VBScript and JScript define arrays differently.
VBScript and JScript arrays have different implementations and thus aren't compatible. VBScript arrays are safearrays, which use a basic COM subtype to provide a safe environment for you to work with an array's bounds and contained data. JScript arrays are pure objects with complex subtypes that you define and shape at runtime. As a result, JScript arrays are more flexible and versatile than VBScript arrays, but because JScript arrays aren't safearrays, you can make scripting errors more easily.
You can use specialized conversion functions to convert VBScript arrays to JScript arrays. No conversion functions exist to convert JScript arrays to VBScript arrays