Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 13, 2005

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

RSS
View this exclusive article with VIP access -- click here to join |
See More VBScript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

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 log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

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

Where is Microsoft NetMeeting in Windows XP?

...


Related Articles Function Finds the nth Occurrence of Substrings

Related Events Virtual Desktop Infrastructure (VDI): Technologies, Differences, Successes

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing