Creating Dynamic Arrays

Microsoft® Windows® 2000 Scripting Guide

A dynamic array offers two advantages to script writers:

  • You do not have to specify the maximum size of the array in advance.

  • The size of the array can change during the course of the script.

For example, you might have a script that reads computer names from a text file. Rather than guess at the number of names in the text file, you can use a dynamic array to store these names. That way, you can create an array, read in the first name, and store it as the first item in the array. If a second name happens to be in the file, you can resize the array and then read in and store the second name. You can then repeat this process until every name has been read and stored.

To create a dynamic array, you must do two things. First, when you declare the array, do not specify a maximum size. Instead, simply use empty parentheses, like this:

Dim arrTestArray()

Second, before you add a new item to the array, use the ReDim Preserve statement and increment the size of the array by 1. The ReDim Preserve statement performs two tasks:

  • The ReDim portion resizes the array.

  • The Preserve portion ensures that no data is lost when the array is resized. This is important. If you simply use the ReDim statement by itself, all the data in your array will be lost when the array is resized.

For example, the following code sample creates a dynamic array named arrTestArray. The script creates a variable named intSize and assigns this the value 0. The script then uses the resultant line of code (intSize = 0) to resize arrTestArray so that it accepts one element (1- 1 = 0).

After the script has assigned a value to the first element (item 0) in the array, ReDim Preserve is used to resize the array so that it accepts two elements (intSize + 1). A value is then assigned to the second element:

Dim arrTestArray()
intSize = 0
ReDim Preserve arrTestArray(intSize)
arrTestArray(intSize) = "A"
ReDim Preserve arrTestArray(intSize + 1)
arrTestArray(intSize + 1) = "B"

To illustrate how you might use ReDim Preserve in an actual system administration script, the following code sample retrieves a list of all the services installed on a computer and stores those service names in a dynamic array. To perform this task, the script must:

  1. Create a dynamic array named arrTestArray.

  2. Create a variable named intSize and assign this variable the value 0.

  3. Connect to the WMI service and retrieve a list of the installed services.

  4. For each service in the returned collection:

    1. Use the ReDim Preserve statement to set the size of arrTestArray to the value stored in intSize. For the first service in the collection, intSize is equal to 0. The array arrTestArray will thus have a size of 0, meaning it can contain one element.

    2. Assign the name of the service (objService.DisplayName) to the array element just created.

    3. Increment the size of intSize. For example, after the first element has been assigned a value, intSize will be changed to 1 (the original value, 0, plus 1).

    4. Repeat the step for the remaining services in the collection. For the second service, ReDim Preserve sets the size of the array to 1, allowing two elements to be contained within the array. For the third service, ReDim Preserve sets the size to 2, allowing three elements to be stored in the array.

Dim arrTestArray()
intSize = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service")
For Each objService in colRunningServices
    ReDim Preserve arrTestArray(intSize)
    arrTestArray(intSize) = objService.DisplayName
    intSize = intSize + 1
Next