Solved

Grabbing Line within a Text File

Posted on 2006-11-05
4
211 Views
Last Modified: 2010-04-23
I had a similar question like this before and thought I had it working but I cant.
I have a text file, that I'll read and I want to display all the lines below a particular line heading. For instance, if I type inside a texbox "Firmware3" is want it to grad all the lines below it and "Firmware4". And if I type in "Firmware4" I want all the lines between it and "Firmware8".


Firmware3
- Resolves issue with setup wizard
- Updated wireless security tab

Firmware4
- Adds SecureEasySetup push button support
- Resolves large file transfer issues

Firmware8
- Supports version 2 hardware

I've tried several ways but here's my latest. Please help.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sr As New IO.StreamReader("c:\test.txt")
        Dim linein As String
        Dim Result As Integer
        linein = sr.ReadLine
        Do Until linein Is Nothing
            Result = InStr(linein, "Firmware3")
            If Result <> -1 Then
                TextBox1.Text = (linein)
            End If
            linein = sr.ReadLine()
        Loop
        sr.Close()

    End Sub
0
Comment
Question by:DFCRJ
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17877025
Hi DFCRJ,

Looking at your example. Does each line within a group starts with a - character? An those lines that are headers such as Firmware3 do not start with a - character?

Fernando
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17877105
Hi DFCRJ,

This code will work if the answer to my questions to my first post it true.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim sr As New IO.StreamReader("c:\temp\test.txt")
        Dim linein As String
        Dim Result As Integer
        linein = sr.ReadLine
        Do Until linein Is Nothing
            If linein.StartsWith("Firmware4") Then
                TextBox1.Text = (linein) & vbCrLf
                While sr.Peek <> -1
                    linein = sr.ReadLine()
                    If linein.Length > 0 AndAlso linein.Substring(0, 1) = "-" Then
                        TextBox1.AppendText(linein & vbCrLf)
                    Else
                        Exit While
                    End If
                End While
                Exit Do
            End If
            linein = sr.ReadLine()
        Loop
        sr.Close()

    End Sub

Also the old VB 6 InStr(linein, "Firmware3") return a 0 if the string was not found and not a -1. In your code the value of result will always be 0 or greater.

Fernando
0
 

Author Comment

by:DFCRJ
ID: 17878381
Dude your AWESOME!!
I've been fiddling around with that for hours and was getting frustrated. That worked perfect!!
thanks so much..
RJ
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17878651
No problem RJ, always glad to help. ;=)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question