Converting a Delimited String to an Array

Microsoft® Windows® 2000 Scripting Guide

To make administrative data readily accessible to a wide variety of applications (including scripts), this data is often saved in plain-text files and in delimited format. Delimited simply means that a unique character separates the individual fields for each record in the file. This character is known as a delimiter and is typically the comma. These files are often referred to as comma-separated-values files because the individual values are separated by commas.

For example, a log file that lists the name of a server, the name of an event log on that server, and the number of error events that were recorded in that event log might look like this:

atl-dc-01,DNS Server,13
atl-dc-01,System,14
atl-dc-02,Security,3
alt-dc-02,Application,22

One advantage of delimited files is that they can be imported into applications that can automatically parse each line into a single record with multiple fields. For example, an application such as Microsoft Excel or Microsoft Access would parse the sample file like the example shown in Table 2.18.

Table 2.18 Parsing a Sample Data File into Fields

Field 1

Field 2

Field 3

atl-dc-01

DNS Server

13

atl-dc-01

System

4

atl-dc-02

Security

3

atl-dc-02

Application

22

The Split function can be used to convert a delimited string to an array. This is done by passing the function two parameters:

  • The delimited text string.

  • The delimiter.

For example, in the following script the delimited text string is the variable TestString, and the comma serves as the delimiter:

TestString = " atl-dc-01,DNS Server,13"
TestArray = Split(TestString , ",")
For i = LBound(TestArray) to UBound(TestArray)
    Wscript.Echo TestArray(i)
Next

When this script is run under CScript, the individual data fields are echoed to the screen:

atl-dc-01
DNS Server
13