TextInputStream Do Loop Question

Good morning everyone,

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

                    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


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?

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


Sorry, you said “Free” in the string, so change my If line = "Free" Then to If line.InStr("Free") > 0 Then

I believe you said if “Free” was NOT in the line read in end the loop, so this should work, I think:

[code]Dim tis As TextInputStream
Dim line as string

While Not tis.EOF
line = tis.ReadLine

If line.InStr(“Free”) = 0 Then
Exit While
End If

ListBox1.AddRow line


I just came back from vacation. I think I need a vacation to recoup.

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!

Thing is you have no idea you ARE on the last line until you read it and then the EOF indicator is set

so read the line, and just don’t process it

While Not tis.EOF
  line = tis.ReadLine

  if tis.eof then exit while

  If line.InStr("Free") = 0 Then
    Exit While
  End If

  ListBox1.AddRow line

If the file is not big you can do something like:

Dim lines() As String = ReplaceLineEndings(tis.ReadAll, EndOfLine).Split(EndOfLine)
For i As Integer = 0 To lines.UBound - 2
  ... code ...

but I wouldn’t do that for a file of any size.

You could also do something like:

Dim line As String

If Not tis.EOF Then
    line = tis.ReadLine
End If

While Not tis.EOF
  .. code ..

  line = tis.ReadLine

P.S. Dave’s solution is much more elegant, tunnel vision syndrome on my part :smiley:

  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)
  for Position as integer = 0 to leTexte.Ubound
    Position = Position+1
    if (InStr(leTexte(Position), vac)+InStr(leTexte(Position), RO))  = 0 then exit

Could be slightly faster, as the file loads in one piece…