Parsing Passed Text into an Array

上一篇 / 下一篇  2010-03-28 22:12:25 / 个人分类:VBScript

In our script, SearchTXT.vbs, you create a dynamic array and set its initial size to zero. You next make a connection to the file system object and open the Setuplog.txt file, located in the Windows directory (this path may be edited if required), for reading. Once the Setuplog.txt file is opened for reading, you define a search string of "Error" and use theInStrcommand to look through each line. If the string "Error" is found on the line being examined, the line with the error is added to the array. You then increment the next element in the array in case you find another line with the string "Error" in it. After you go through the entire text file, you use aFor...Nextloop and echo out each element of the array. The script. concludes with a friendly "all done" message.
 
 Table 5-1. Variables declared in SearchTXT.vbs

Variable

Use

arrTxtArray()

Declares a dynamic array

myFile

Holds the file name of the file to open up

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 incrementintSizecounter

SearchTXT.vbs

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")
 
 
 
 

Quick Check

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.


TAG:

 

评分:0

我来说两句

Open Toolbar