I want to read a file starting from the last line in the page
I am trying to find a way to read a file starting from the last line in the page, reason why I Want to do that, because this file is always updated and I am watching this file to do something when it find an specific errors. The word could be found in more then one place in the file, by reading the last line this way I don't have to worry if this same error existed before in the file.
Any help is appreciated.
Thanks
Re: I want to read a file starting from the last line in the page
Try the following - hopefully it works for you:
Code:
Dim sFile As String
Dim sText As String
Dim arLines() As String
Dim LastLine As String
Dim i As Long
sFile = App.Path & "\mydata.txt"
Open sFile For Input As #1
sText = Input(LOF(1), #1)
arLines = Split(sText(, vbNewLine))
Close #1
sText = ""
LastLine = arLines(UBound(arLines))
Debug.Print arLines
'or loop if you need to
For i = UBound(arLines) To LBound(arLines) Step -1
Debug.Print arLines(i)
Next i
Re: I want to read a file starting from the last line in the page
Thanks Rhino, I am getting an error on sTEXT, error msg "Expected Array"
why is that? i Place this code in COMMAND CLick
Re: I want to read a file starting from the last line in the page
Ehhh... there is a typo:
repalce this: arLines = Split(sText(, vbNewLine))
with this: arLines = Split(sText, vbNewLine)
Sorry about that.
Re: I want to read a file starting from the last line in the page
Quote:
Originally Posted by RhinoBull
repalce this: arLines = Split(sText(, vbNewLine))
I thought you had come up with an undocumented way of using Split. :D
Re: I want to read a file starting from the last line in the page
Hah, I guess I just did. :p
Re: I want to read a file starting from the last line in the page
Quote:
Originally Posted by wajdi
I am trying to find a way to read a file starting from the last line in the page, reason why I Want to do that, because this file is always updated and I am watching this file to do something when it find an specific errors. The word could be found in more then one place in the file, by reading the last line this way I don't have to worry if this same error existed before in the file.
Any help is appreciated.
Thanks
If you are regularly rechecking the *SAME* file for changes, I suggest you do this:
1) Store LOF (length of file) for the file after each check
2) ONLY load in new data from the point stored in the previous LOF to the current LOF...if these two values match you know it hasn't changed, of course and can just ignore the check
This will greatly speed up your checking because you won't have to recheck stuff time and time again
This is easy to do and if you require further help, people here will help you with "random access" to files and how exactly to achieve what I suggested above. I could do it, but I dunno if I will be around :-)
Re: I want to read a file starting from the last line in the page
Thanks guys for your help, RIHNO, why am I getting in at this line "' Debug.Print arLines" Type Mismatch?
Re: I want to read a file starting from the last line in the page
You can remove that line...any debug.print lines are used for debugging and it's just showing you what the result is...
...however, if you want to fix it, change the "arLines" to "arLines(UBound(arLines))"
Re: I want to read a file starting from the last line in the page
THanks sMUX, I am a bit comfused here, how I use this code to look for "updates detected"?
Re: I want to read a file starting from the last line in the page
My suggestion is to go back to the original post I made...if you were looking for the word "five" in this string with the code I suggested you write (or ask someone to help with):
"onetwothreefourfivesixseven"
it would find it the first time then would take the length of this string and remember it. Next time it checks for "five" it would look in:
"onetwothreefourfivesixseveneightninetenonetwothreefourfivesix"
it would start at the end of "seven" and find the *second* "five"
Re: I want to read a file starting from the last line in the page
Quote:
Originally Posted by wajdi
...why am I getting in at this line "' Debug.Print arLines" Type Mismatch?
because it was free hands typing thing ... :)
Anyway you need to learn how to fix this sort of error - the index is missing... :blush:
Use this instead:
Debug.Print arLines(UBound(arLines))
Re: I want to read a file starting from the last line in the page
This project should help you do what you want to do. Look at the top of the page for the Automatic Log File Updater