Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

July 01, 1999 12:00 AM

Using VBScript Arrays in Scripts

Windows IT Pro
InstantDoc ID #5628
Rating: (1)
Downloads
5628.zip

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.

The Example
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

ReDim Preserve arrText(UBound(arrText) + 1)

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.

The Warning
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

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Jul 13, 2005

    Thanks! It worked like a charm.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.