Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InStr not finding last line of text file

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
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
 
Your code is fine for looping through. It is getting to the end of the file. Take a look at your code to look for the day of the week.

Run this and I think you will understand.
Code:
WScript.Echo WeekDay(Now())

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Hi Mark, I'm not sure I understand. The text file I am searching is an appending file and what I am doing is just counting the lines today for yesterday. The code works fine but if there are let's say 26 lines with yesterdays date, the code returns a count of 25. This seems to happen for any day of the week. It always seems to be one short.
 
Hi,

It is critical that you check, in minute detail, what happens at you limits.

Are you actually reading the very last line? Look carefully!
 
Uggh...Now I am getting confused. I ran the same code and used a different text file and it is working and counting the lines correctly. I just can't see what should be different in my code.
 
Maybe you don't understand in minute detail what is happening at you limit. It's got to work under all conditions!
 
I think you are right, I don't completely understand what is happening. Sometimes the count is off by one and most of the time it is totally accurate. Please help me understand this code.
 
are you using the Step feature and the Watch Window to observe what is happening to key variables?
 
When your code is once again off by one, check whether ALL lines in the text file contain "Ended", case sensitive, since this is what you use to determine the line to be counted.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Perhaps it's lack of coffee, but
a) I don't see the problem in OP's code
b) I don't see the limit problem that Mark or Skip seem to see
 
On a separate note, I would drop the "On Error Resume Next" line ... it will suppress runtime errors which could lead to quite unexpected results.
 
I did not see anything specific.

I only know that when I process a loop, I often need to pay careful attention to the limits.
 
I think my lack of sleep had me barking up the wrong tree. I was looking at the WeekDay(Now) and thinking that results in a number but failed to take into account the WeekDayName being used. Apologies for misdirecting attention.

I stripped out some code to test with a source file and just used an echo for each line using what the OP posted and in my test it did echo out the last line, so I don't see a problem with the OP's looping through the lines of text.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Thanks to all for the help. This seems to be working, but like Guitarzan pointed out, I need to remove "On Error Resume Next". Maybe that was causing the problem for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top