Reading Text Files
Method | Description |
---|---|
Read | Reads the specified number of characters and then stops. For example, the following command would read the first 12 characters of the first line ("Alerter,Shar") into the variable strText and then stop:strText = objTextFile.Read(12) |
ReadLine | Reads one line from a text file and then stops before reaching the newline character. For example, the following command would read the first line ("Alerter,Share Process,Running,Auto,LocalSystem") into the variable strText and then stop: To read an entire text file on a line-by-line basis, place the ReadLine function within a loop. |
ReadAll | Reads the entire contents of a text file into a variable. |
Skip | Skips the specified number of characters and then stops. For example, the following command would skip the first 12 characters. Any subsequent read operations would begin with the 13th character and continue from there ("e Process,Running,Auto,LocalSystem"):objTextFile.Skip(12) |
SkipLine | Skips an entire line in a text file. For example, this code reads the first and third lines of a text file, skipping the second line: |
Listing 4.37 Verifying the Size of a File
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile("C:\Windows\Netlogon.log") If objFile.Size > 0 Then Set objReadFile = objFSO.OpenTextFile("C:\Windows\Netlogon.log", 1) strContents = objReadFile.ReadAll Wscript.Echo strContents objReadFile.Close Else Wscript.Echo "The file is empty."
End If
Listing 4.38 Reading a Text File Using the ReadAll Method
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", ForReading) strContents = objFile.ReadAll Wscript.Echo strContents objFile.Close
Listing 4.39 Reading a Text File Using the ReadLine Method
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ServerList.txt", 1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine Wscript.Echo strLine Loop objFile.Close
Listing 4.40 "Reading" a Text File from the Bottom to the Top
Dim arrFileLines()
i = 0 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1) Do Until objFile.AtEndOfStream Redim Preserve arrFileLines(i) arrFileLines(i) = objFile.ReadLine i = i + 1 Loop objFile.Close For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 Wscript.Echo arrFileLines(l) Next
6/19/2002 Success 6/20/2002 Failure 6/21/2002 Failure 6/22/2002 Failure 6/23/2002 Success
6/23/2002 Success
6/22/2002 Failure 6/21/2002 Failure 6/20/2002 Failure 6/19/2002 Success
Listing 4.41 Reading a Fixed-Width Text File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1) Do Until objFile.AtEndOfStream objFile.Skip(5) strCharacters = objFile.Read(1) Wscript.Echo strCharacters objFile.SkipLine Loop
C:\FSO\ScriptLog.txt
XXXXX1XXXXXXXXXXXXXX XXXXX2XXXXXXXXXXXXXXXXXXX XXXXX3XXXXXXXXXXXX XXXXX4XXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4