×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

InStr not finding last line of text file

InStr not finding last line of text file

InStr not finding last line of text file

(OP)
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 

RE: InStr not finding last line of text file

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 --> VBScript

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 http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: InStr not finding last line of text file

(OP)
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.

RE: InStr not finding last line of text file

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!

RE: InStr not finding last line of text file

(OP)
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.

RE: InStr not finding last line of text file

Maybe you don't understand in minute detail what is happening at you limit. It's got to work under all conditions!

RE: InStr not finding last line of text file

(OP)
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.

RE: InStr not finding last line of text file

are you using the Step feature and the Watch Window to observe what is happening to key variables?

RE: InStr not finding last line of text file

blush so sorry I'm thinking of VBA. blush

RE: InStr not finding last line of text file

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.

RE: InStr not finding last line of text file

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

RE: InStr not finding last line of text file

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.

RE: InStr not finding last line of text file

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.

RE: InStr not finding last line of text file

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 http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: InStr not finding last line of text file

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close