Solved

Grabbing Line within a Text File

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

12 Experts available now in Live!

Get 1:1 Help Now