?
Solved

Grabbing Line within a Text File

Posted on 2006-11-05
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 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