
Processing flat file that is always appending
I recently offered a solution to a similar problem in this newsgroup with
the subject "line match in file".
I've modified that solution for you below.
This supports either "ADO" or "FSO" based on "cTYP".
Modify "cFIL" to point to your file.
Modify "cTXT" to have your search string..
It should work as is.
(Watch for word wrap especially in the comment header)
Option Explicit
'====================================
=======================================
' "Qgrepper.vbs" Date Created: 07-Feb-2003 Date Updated:
12-Feb-2003
'
' This VBS (Visual Basic Script) program does the following:
' 1) Specify "cFIL" = a text file to be examined.
' 2) Specify "cTXT" = text to find within a line.
' 3) Open the file and examine the records using "ADODB.Stream".
' 4) Report only the first instance found.
'
' To test, either double-click on the filename in Windows Explorer or
' from the Command prompt, type "cscript.exe //nologo Qgreping.vbs".
'
' Changes:
' ---------------------------------------- --------------------------------
-
' 12-Feb-2003.1 Changed.
' 07-Feb-2003.0 Created.
'
'==========================
=================================================
Const cVBS = "Qgrepper.vbs"
Const cFIL = "Qgrepper.vbs"
Const cTXT = "Explicit"
' Const cTYP = "ADO"
Const cTYP = "FSO"
'*
'* Start Message
'*
MsgBox "'" & cVBS & "' started."
'*
'* Processing
'*
Call Processing()
'*
'* Finish Message
'*
MsgBox "'" & cVBS & "' finished."
Sub Processing()
'*
'*
'*
WScript.Echo "'" & cFIL & "' grep via '" & cTYP & "'"
Dim strDAT
If cTYP = "ADO" Then
'*
'* Declare Objects, LoadFromFile, Cleanup Objects
'*
Dim objADO
Set objADO = CreateObject("ADODB.Stream")
objADO.Open
objADO.Type = 2 '= adTypeText
objADO.Charset = "ascii"
objADO.LoadFromFile(cFIL)
WScript.Echo "'" & cFIL & "' = " & objADO.Size & " bytes."
strDAT = objADO.ReadText
objADO.Close
Set objADO = Nothing
Else
'*
'* Declare Objects, ReadAll, Cleanup Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objGFI
Set objGFI = objFSO.GetFile(cFIL)
WScript.Echo "'" & cFIL & "' = " & objGFI.Size & " bytes."
Set objGFI = Nothing
Dim objOTF
Set objOTF = objFSO.OpenTextFile(cFIL,1)
strDAT = objOTF.ReadAll()
objOTF.Close
Set objOTF = Nothing
Set objFSO = Nothing
End If
'*
'*
'*
Dim arrDAT
arrDAT = Split(strDAT,vbCrLf)
WScript.Echo "'" & cFIL & "' = " & UBound(arrDAT) & " lines."
'*
'* Declare Variables
'*
Dim intTXT
Dim strTXT
strTXT = "'" & cFIL & "' does not contain '" & cTXT & "'"
'*
'* Examine File
'*
For intTXT = 0 To UBound(arrDAT)
If InStr(arrDAT(intTXT),cTXT) > 0 Then
strTXT = "'" & cFIL & "' contains '" & cTXT & "' on line " &
intTXT+1 & ":"
strTXT = strTXT & vbCrLf & vbCrLf & arrDAT(intTXT)
Exit For
End If
Next
WScript.Echo strTXT
End Sub
Quote:
> I need to be able to read from an ever increasing regular
> text file, such that I need to process each line looking
> for text etc. In the world I know I'd use this shell
> tail -f filename | program.
> Right now my wsh script is looping, making a copy,
> sleeping, doing a diff and then string matching the new
> lines. This worked fine until the file got to 20 MEG. I
> need to do this more efficiently.
> Any tips would be appreciated.
> Thanks