The code listed below is working as it should, however it is doing more work (taking more time) than I need it to. As you can see, I currently use textinput.EOF to end the Do loop. What I need to do is to end the do loop when the first line encountered in the text file that I am processing does NOT contain the string “Free” (which is usually about half way through the file).
DIM textInput As TextInputStream
DIM vac as String
DIM RO as String
Do
dim rowFromFile as String = textInput.ReadLine
if InStr(rowFromFile, vac) > 0 or InStr(rowFromFile, RO) >0 Then
ListBox1.AddRow rowFromFile
end if
loop until textInput.EOF
textInput.Close
How would I program my do loop to stop when the first line in the text file its processing does NOT contain a certain string instead of using EOF as the loop terminator?
Thanks
I would not use a Do/Loop in this case, what if the file is simply empty? Also, I do not understand what the variable “vac” or “RO” are for, they are empty but you are doing an InStr on it?
Here is what I would do:
Dim tis As TextInputStream
While Not tis.EOF
Dim line As String = tis.ReadLine
If line = "Free" Then
Exit While
End If
ListBox1.AddRow line
Wend
tis.Close
Thanks for the assistance, the code above worked as expected.
One new problem has surfaced, instead of reading the file to the end (EOF) i need the program to ignore the very last line and not attempt to search for the programmed values within that line (I want the program to ignore the last line) so something like EOF -1.
The text files that I am processing can range from 40k lines to 120k lines and the last line in the files is never needed.
I have found that the last line in the the text file that i am processing is simply full “NULL” characters and this adds significant time to the processing of the file as the program reads this line and checks to see if the values specified in the program are found which they will never be.
How can I program my while loop to process all lines but ignore the last line and perform no processing on this line?
BTW…I have manually deleted the last line from the file and re-processed it with the above program and the time savings in significant.
Thanks again for all of the help!
dim f as FolderItem = specialfolder.Desktop.child("free.txt")
DIM textInput As TextInputStream = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman
DIM vac as String = "free"
DIM RO as String //Don't know what it contains
Dim LeTexte(-1) as string
leTexte = split(textinput.readall,EndOfLine.Macintosh)
textInput.close
for Position as integer = 0 to leTexte.Ubound
ListBox1.AddRow(leTexte(Position))
Position = Position+1
self.refresh
if (InStr(leTexte(Position), vac)+InStr(leTexte(Position), RO)) = 0 then exit
next
Could be slightly faster, as the file loads in one piece…