What I'm trying to do is take a source file and only pull specific lines out and display those lines to a label/textbox/etc. What's happening is I am able to pull the first line, however my loop is terminating and I do not receive the rest of the results. Here is what the source will look like:

text1 = text1
text2 = text2
text3 = text3
text4 = text4
text5 = abcdefg (Yes)
text6 = hijklmn (Yes)
text7 = 1234567 (No)
text8 = abcdefg (No)

What I want to pull is any data that is marked Yes, so in this case abcdefg and hijklmn.

Here is the coding I have so far:

        Dim stream_reader As New StreamReader("c:\sample.txt")
        Dim file_contents As String = stream_reader.ReadToEnd()
        Dim count As Integer

        stream_reader.Close()

        count = file_contents.IndexOf("(Yes)")
        file_contents = file_contents.Replace(vbLf, "")

        file_contents = file_contents.Substring(count)
        Label4.Text = file_contents

From here, it will only return almost what I'm looking for, however it's just not it. Any help would be appreciated. Thank you!

Recommended Answers

All 2 Replies

A short version would be

Dim txt() As String = Filter(System.IO.File.ReadAllLines("d:\temp\test.txt"), "(Yes)")
TextBox1.Text = Join(txt, vbCrLf)

A shorter version is

TextBox1.Text = Join(Filter(System.IO.File.ReadAllLines("d:\temp\test.txt"), "(Yes)"), vbCrLf)

Here is what the source will look like:
text1 = text1
text2 = text2
text3 = text3
text4 = text4
text5 = abcdefg (Yes)
text6 = hijklmn (Yes)
text7 = 1234567 (No)
text8 = abcdefg (No)

What I want to pull is any data that is marked Yes, so in this case abcdefg and hijklmn.

   'get all lines in the text file
   Dim text() As String = System.IO.File.ReadAllLines("sample.txt")

   'create an array of only those lines that contain "(Yes)"
   Dim YesLines() As String = Array.FindAll(text, Function(str As String) str.Contains("Yes")).ToArray
   ' or
   'Dim YesLines() As String = text.Where(Function(str As String) str.Contains("Yes")).ToArray


   'Process the lines to remove unwanted text (Text# = ) and (Yes)
   '1st cut-off "Text# =" using SubString
   'then replace (Yes) with nothing
   'finally trim leading and trailing spaces
   Dim WantedText() As String = YesLines.Select( _
                                   Function(str As String) _
                                   str.Substring(str.IndexOf("=") + 1).Replace("(Yes)", "").Trim _
                                               ).ToArray()

   'You now have the data you want in a string array
   'If you need this array as a single string
   Dim oneline As String = String.Concat(WantedText)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.