Solved

Grabbing Line within a Text File

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update textboxes with Streamreader 21 47
Help with LINQ and XML 10 36
Modal Popup Extender control 1 20
HTML - Color not displaying correctly in EMAIL. 6 36
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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