Solved

Grabbing Line within a Text File

Posted on 2006-11-05
4
204 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 62

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 62

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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now