Reading Text Files in WSH - Advanced concepts
(Page 4 of 4 )
Simply reading a text file is generally not enough. You will usually need to be able to work with the data you find. This is called parsing. Parsing can be an extremely arduous task depending on the type of data you are working with and how you want to manipulate it.
I want to present a couple of quick advanced techniques that you will probably find useful as well as a couple of tips to keep in mind when developing your own solutions.
You can only read text files from top to bottom in one pass. There is no way to move back up through a text file.
Text files, especially log files, can become very large, very quickly. If you are processing large amounts of information it can be helpful to know what size your text file is before you begin—more specifically to know that it’s not empty! Here is a modification to our example that employs a little error checking to make sure our text file actually contains data to prevent Read errors.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = GetFile(“C:\addresses.txt”)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile("C:\addresses.txt", 1)
Do Until objReadFile.AtEndOfStream
strContents = strContents & objFile.Read(1)
Loop
Wscript.Echo strContents
objFile.Close
Else
Wscript.Echo “File is empty.”
End If
Frequently you will be reading text files for the purpose of finding specific data. Often this is much easier if we read our text file into an array first. In this example we are going to return the fourth record from our example text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = GetFile(“C:\addresses.txt”)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile("C:\addresses.txt", 1)
i = 1
Do Until objReadFile.AtEndOfStream
Redim Preserve arrData(i)
arrData(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
strContents = arrData(4)
End If
One of the most common uses for reading a text file is to check logged data. Frequently that data is updated by appending information to an existing file which means that the newest data is at the bottom.
If you were reading a text file and wanted to know the most recent action, you would first have to read through the whole file to find the last line. In cases like this it is much easier to process the file in reverse order.
Watch how we can modify our array example so that it processes a file one line at a time from the bottom up.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = GetFile(“C:\addresses.txt”)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile("C:\addresses.txt", 1)
i = 1
Do Until objReadFile.AtEndOfStream
Redim Preserve arrData(i)
arrData(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
For l = Ubound(arrData) to LBound(arrData) Step -1
Wscript.Echo “Record”, l, “=”, arrData(l)
Next
End If
That wraps up part one of this three part series. In the next part I’ll show you how to write to text files. So, until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |