Reading file and "if....then"

While I reading a txt file into a listbox, how can I halt the process if a line has a certain text

Dim myfile as FolderItem
Dim d as OpenDialog
Dim t as TextInputStream
d=New OpenDialog
myFile=d.ShowModal
ListBoxTodo.DeleteAllRows

If myFile<>Nil Then
t=TextInputStream.Open(myFile)
While Not t.EOF

if t.ReadLine="Money" Then
  Exit
Else
  
  ListBoxTodo.AddRow(t.ReadLine)
End if

Wend

Dim i as Integer

Redim ToDoList(-1)

For i = 0 to ListboxTodo.Listcount -1
ToDoList.Append(ListboxTodo.list(i)) // assign a name to each index in the array
next

End If

Thank you!

Do you know about InStr and InStrB ?

At a quick glance that looks right.
Whats the problem?

Change the code to be this:

t=TextInputStream.Open(myFile) dim v as string While Not t.EOF v = t.ReadLine if trim(v) ="Money" Then Exit ..etc

The TRIM statement will handle cases where the line is "Money "
If you want to stop is a line BEGINS with “Money”, use this:

if Left(v,5) =“Money” Then

Emile and Jeff,
Thank you! I have tried both your methods. While naturally they appear sound, it is still not working.

What I wanted was for the lines prior to ‘Money’ from the .txt file being read into the listbox ; but, not ‘Money’ or any other line after it.

The output of my code is not predictable.

Should I explicitly declare what type of characters )unicode etc) are being read?

Thanks

Your original code, as posted, will only put every second line into the listbox. You should be sure that you do as Jeff suggested and read each line into a variable.

Further, if the second of your readline calls happens to be the one that encounters “Money”, then it’ll put that into the listbox and you’ll continue reading up to EOF.

I don’t know what happened, but the reply I posted did not show, while in the main list my name appeared…
Reposting.

As Jeff and Tim already said, put into a string each line you are going to evaluate.

Apart from that, after myfile(open) I would add:

t.encodings = encodings.UTF8

especially if you are going to deal with Indic text.
Unless the .txt file has a different encoding, or its line endings are not the expected ones.

[quote=447083:@Tim Streater]Your original code, as posted, will only put every second line into the listbox. You should be sure that you do as Jeff suggested and read each line into a variable.

Further, if the second of your readline calls happens to be the one that encounters “Money”, then it’ll put that into the listbox and you’ll continue reading up to EOF.[/quote]

Maybe to clarify Tim’s reply, each time you call t.ReadLine it reads a new line, so the line you are reading in your if… then block is different from the line you are actually adding to the listbox.

Tim has also explained how to solve your problem.

Julen

(I had previously posted a reply but has not appeared; Iam reposting it below)

Emile, Jeff, Tim, Carlo and Julen,
thank you for taking time to sharing your expertise. I have made changes suggested your posts and it now works. You have made my day! Wishing you a great weekend!

The modified code that works:

Dim myfile as FolderItem
Dim d as OpenDialog
Dim t as TextInputStream
d=New OpenDialog
Dim enc As TextEncoding
myFile=d.ShowModal
ListBoxTodo.DeleteAllRows

If myFile<>Nil Then
t=TextInputStream.Open(myFile)

enc = t.Encoding ’

While Not t.EOF
dim v as string

dim s as integer
v=t.ReadLine
v=trim(v)

if Left(v,5)="Money" then 
  Exit
  
  
Else
  
  ListBoxTodo.AddRow(v)
end if

Wend

Dim i as Integer

Redim ToDoList(-1)

For i = 0 to ListboxTodo.Listcount -1
ToDoList.Append(ListboxTodo.list(i)) // assign a name to each index in the array
next

End If

Ganesalingam,

Dim enc As TextEncoding//remove it
myFile=d.ShowModal
ListBoxTodo.DeleteAllRows
If myFile<>Nil Then
t=TextInputStream.Open(myFile)
enc = t.Encoding '//remove it

I’d re-write the above snippet as shown in the example in Language Reference > TextInputStream:

myFile=d.ShowModal
ListBoxTodo.DeleteAllRows
If myFile<>Nil Then
t=TextInputStream.Open(myFile)
t.encodings = encodings.UTF8

And why not do everything in one go? No more need to build the array at the end.

ListBoxTodo.DeleteAllRows
Redim ToDoList(-1)
If myFile<>Nil Then
// your code above, until:
Else
ListBoxTodo.AddRow(v)
ToDoList.Append v
end if
wend

Neat. Thank you!