I have some code here to count lines containing the previous days date and a word. I am hoping someone can tell me what is wrong with my code. For some reason when I search for the previous day (which is usually the last several lines of the text file), the code does not see the last line of the file and appears to ignore it. Maybe I am missing something?
Code:
On Error Resume Next
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True
Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines, ask, objLogFile
Set WshShell = CreateObject("WScript.Shell")
strDir = WshShell.CurrentDirectory & "\"
If WeekDayName(WeekDay(Now())) = "Monday" Then
strSearchFor = Date - 3
Else
strSearchFor = Date - 1
End If
strSearchWrd = "Ended"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strDir & "Program Run Log.txt", ForReading)
LineCount = 0
do until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine()
If InStr(strLine, strSearchFor) <> 0 then
If InStr(strLine, strSearchWrd) <> 0 then
LineCount = LineCount + 1
End If
End If
loop
MsgBox "Run Date: " & strSearchFor & vbCrlf & vbCrlf & "Total Boards Processed: " & LineCount,, "Board Count"
objTextFile.Close
Set objLogFile = objFSO.OpenTextFile(strDir & "Daily.Run.Total.txt", ForAppending, True)
objLogFile.Write "Run Date," & strSearchFor & ",Total Boards," & LineCount & vbCrlf
objLogFile.Close