Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


June 13, 2005

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

RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Articles Function Finds the nth Occurrence of Substrings

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Take Control of Storage & eDiscovery Costs with Unified Content Archiving

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement