Option Explicit
On Error Resume Next
Dim arrTxtArray()
Dim myFile
Dim SearchString
Dim objTextFile
Dim strNextLine
Dim intSize
Dim objFSO
Dim i
intSize = 0
myFile = "c:\windows\setuplog.txt" <'>Modify as required
SearchString = "Error"
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile _
(myFile, ForReading)
Do until objTextFile.AtEndOfStream
strNextLine = objTextFile.ReadLine
if InStr (strNextLine, SearchString)then
ReDim Preserve arrTxtArray(intSize)
arrTxtArray(intSize) = strNextLine
intSize = intSize + 1
End If
Loop
objTextFile.close
For i = LBound(arrTxtArray) To UBound(arrTxtArray)
WScript.Echo arrTxtArray(i)
Next
WScript.Echo("all done")
ParseAppLog.vbs
Table 5-2. Variables declared in ParseAppLog.vbs
Variable | Use |
---|
arrTxtArray() | Declares a dynamic array |
appLog | Holds the file name of the file to open |
SearchString | Holds the string to search for |
objTextFile | Holds the connection to the text file |
strNextLine | Holds the next line in the text stream |
intSize | Holds the initial size of the array |
objFSO | Holds the connection to the file system object |
i | Used to increment theintSizecounter |
ErrorString | Holds the second search string used |
newArray | New array created to sort the output |
Option Explicit
On Error Resume Next
Dim arrTxtArray()
Dim appLog
Dim SearchString
Dim objTextFile
Dim strNextLine
Dim intSize
Dim objFSO
Dim i
Dim ErrorString
Dim newArray
intSize = 0
appLog = "applog.csv" <'>Ensure in path
SearchString = ","
ErrorString = "1004"
Const ForReading = 1
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjTextFile = objFSO.OpenTextFile _
(appLog, ForReading)
Do until objTextFile.AtEndOfStream
strNextLine = objTextFile.ReadLine
if InStr (strNextLine, SearchString)Then
If InStr (strNextLine, ErrorString) then
ReDim Preserve arrTxtArray(intSize)
arrTxtArray(intSize) = strNextLine
intSize = intSize + 1
End If
End If
Loop
objTextFile.close
For i = LBound(arrTxtArray) To UBound(arrTxtArray)
If InStr (arrTxtArray(i), ",") Then
newArray = Split (arrTxtArray(i), ",")
WScript.Echo "Date: " & newArray(0)
WScript.Echo "Time: " & newArray(1)
WScript.Echo "Source: " & newArray(2)& " "& newArray(3)
WScript.Echo "Server: " & newArray(7)
WScript.Echo "Message1: " & newArray(8)
WScript.Echo "Message2: " & newArray(9)
WScript.Echo "Message3: " & newArray(10)
WScript.Echo " "
End If
Next
WScript.Echo("all done")
Q. | What is the advantage of using a dynamic array? |
A. | You can expand a dynamic array when a new element is needed. This saves memory and is more efficient. |
Q. | How isReDim Preserveused? |
A. | ReDim Preserveis used to resize a dynamic array while ensuring that the data contained in the array is not lost. |
Q. | What is the simplest way to break up a CSV data stream to populate an array? |
A. | You need to use theSplitcommand and look for commas. |
Q. | What is theInStrcommand used for? |
A. | TheInStrcommand is used to look for character combinations in a stream of text. |
Q. | What construct can be used to hold data records that are separated by commas? |
A. | A multidimensional array can be used to hold this type of data. |