Subscribe to Windows IT Pro

 

Get Newsletters

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

Subscribe Now!

June 13, 2005 12:00 AM

Use the FileToArray Function to Read a Text File Into an Array

Windows IT Pro
InstantDoc ID #46489
Rating: (6)
Downloads
46489.zip

Opening text files is a common scripting task. Through the Microsoft Scripting Runtime Library's FileSystemObject object, you can access the TextStream object, which provides methods and properties for working with text files. Opening text files usually entails a series of steps, such as

  1. Create an instance of the FileSystemObject object.
  2. Use FileSystemObject's OpenTextFile method to create a TextStream object.
  3. Use the TextStream object's methods to read the contents of the file.
  4. Close the file.

Although these steps aren't difficult to implement, I find it convenient to retrieve a text file into an array, so I created the FileToArray function. Reading a file into an array has its advantages. You can use a For Each...Next statement to iterate through all the lines or use an array index to access just one line. You can even iterate through a file multiple times without having to close it and open it again.

Listing 1 shows the FileToArray function. The function first declares its return value to be an empty, single-element array by using VBScript's Split function, as callout A in Listing 1 shows. That way, the function always returns an array, even if an error occurs.

Next, FileToArray creates a FileSystemObject instance and uses the FileExists method to check whether the specified file exists. If the file doesn't exist, the function returns an empty array and ends. If the file exists, the function uses the On Error Resume Next statement to disable VBScript's error handler and uses FileSystemObject's OpenTextFile method to create a TextStream object.

Provided that the OpenTextFile method succeeds, the function uses TextStream's ReadAll method to retrieve the file's contents into a single string variable, then closes the file. Finally, the function uses the Split function to return the file's contents as an array, as callout B in Listing 1 shows.

To use the FileToArray function, you add the code that Listing 1 shows to your script, then call the function using the syntax

FileToArray(strFile, blnUNICODE)

The strFile parameter specifies the name of the file. The blnUNICODE parameter is a Boolean value that specifies whether (True value) or not (False value) the file is in Unicode format. Listing 2 shows an example of how you might call FileToArray in a script. This VBScript code displays the contents of the boot.ini file. Note that the FileToArray function will likely perform poorly on very large text files because it relies on TextStream's ReadAll method to read the entire file into memory at once.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
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.