locked
how to get first line and last line of selecteditem from txt file RRS feed

  • Question

  • i try this code for the first line and last line but for all the txt file data

     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
            'this code to get the first and last line for all txt file
            Dim fl() As String '(First line)
            Dim ll() As String '(last line)
    
            fl = File.ReadAllLines("c:\test.txt")
            ll = File.ReadAllLines("c:\test.txt")
    
            For I As Integer = 0 To LBound(fl) + 1
                TextBox1.Text = (fl(I))
            Next
            For I As Integer = 0 To UBound(ll)
                TextBox2.Text = (ll(I))
            Next
    
        End Sub

    this the example file pic

    i used this code to get first line and last line of group as (si) that  populated in combobox called Comcompanyname3

     'this code to write the list line of the group
    
            Dim Lines As String()
            Lines = File.ReadAllLines("c:\test.txt")
    
    
            For Each line In Lines
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    TextBox1.Text = line
                End If
            Next

    but i got the last line of group si

    1-how to get the first line

    2-how to get the count of the group lines like si = 4

    thank you for your help

    Monday, July 16, 2012 9:44 AM

Answers

  • Here is an example

            Dim Lines As String()
            Lines = File.ReadAllLines("c:\test.txt")
            Dim LineList As New List(Of String)
    
            For Each line In Lines
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    LineList.add(line)
                End If
            Next
            'Put the first one in a text box
            TextBox1.Text = LineList(0)
    
            'Put the last one in a text box
            TextBox2.Text = LineList(LineList.Count - 1)
    
            'Get the number of line that contains "SI"
            Dim NB As Integer = LineList.Count
    

    • Proposed as answer by Paul Ishak Monday, July 16, 2012 5:30 PM
    • Marked as answer by cat2011 Tuesday, July 17, 2012 2:28 PM
    Monday, July 16, 2012 10:08 AM
  • using Crazypennie's code + LINQ:

    'Put the first one in a text box
    TextBox1.Text = LineList.First
    
    'Put the last one in a text box
    TextBox2.Text = LineList.Last


    thanks for any help

    • Marked as answer by cat2011 Tuesday, July 17, 2012 2:29 PM
    Tuesday, July 17, 2012 1:39 AM

All replies

  • Here is an example

            Dim Lines As String()
            Lines = File.ReadAllLines("c:\test.txt")
            Dim LineList As New List(Of String)
    
            For Each line In Lines
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    LineList.add(line)
                End If
            Next
            'Put the first one in a text box
            TextBox1.Text = LineList(0)
    
            'Put the last one in a text box
            TextBox2.Text = LineList(LineList.Count - 1)
    
            'Get the number of line that contains "SI"
            Dim NB As Integer = LineList.Count
    

    • Proposed as answer by Paul Ishak Monday, July 16, 2012 5:30 PM
    • Marked as answer by cat2011 Tuesday, July 17, 2012 2:28 PM
    Monday, July 16, 2012 10:08 AM
  • using Crazypennie's code + LINQ:

    'Put the first one in a text box
    TextBox1.Text = LineList.First
    
    'Put the last one in a text box
    TextBox2.Text = LineList.Last


    thanks for any help

    • Marked as answer by cat2011 Tuesday, July 17, 2012 2:29 PM
    Tuesday, July 17, 2012 1:39 AM
  • thank you for your great help
    Tuesday, July 17, 2012 2:29 PM
  • thank you for your help
    Tuesday, July 17, 2012 2:30 PM
  • hi

    I do not if you allow me to ask again for the same code or i have to ask new question?

    How to get the max value of column by the group

    i used the code above and i add some code but i got error

    this the code

     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
    
            Dim fe As String()
            fe = IO.File.ReadAllLines("c:\test.txt")
            Dim LineList As New List(Of String)
            
            For Each line In fe
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    LineList.Add(line)
                End If
            Next
            ''Put the first one in a text box
            TextBox1.Text = LineList(0)
            'TextBox1.Text = LineList.First '(2 way to get first line using LINQ)
    
            
            ''Put the last one in a text box
            TextBox2.Text = LineList(LineList.Count - 1)
            'TextBox2.Text = LineList.Last '(2 way to get last line using LINQ)
    
            'Get the number of line that contains "SI"
            Dim NB As Integer = LineList.Count
            TextBox4.Text = NB
    
    
            Dim arrayForTheLine = Lines.Split(","c)
            'will convert the following formats date (yyyymmdd),(yyyy/mm/dd),(yyyy-mm-dd) ToShortDateString 
            Dim strDate = If(Date.TryParse(arrayForTheLine(1), Nothing), _
            CDate(arrayForTheLine(1)), CDate(New Date(Val(arrayForTheLine(1).Substring(0, 4)), Val(arrayForTheLine(1).Substring(4, 2)), Val(arrayForTheLine(1).Substring(6, 2))).ToShortDateString))
            Dim strO = arrayForTheLine(2)
            Dim strH = arrayForTheLine(3)
            Dim strL = arrayForTheLine(4)
            Dim strCl = arrayForTheLine(5)
            Dim strV = arrayForTheLine(6)
            TextBox5.Text = maxScore.Date
            TextBox6.Text = maxScore.strh
            TextBox7.Text = maxScore.strl
            TextBox8.Text = maxScore.strcl
            TextBox9.Text = maxScore.strv
    
    
        End Sub

    and i am trying to get the max vale for given lines

    like gd  how to get the the max value of the first 4 lines of the <o>  column ?

    also i try this code but still not working

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
    
            Dim fe As String()
            fe = IO.File.ReadAllLines("c:\test.txt")
            Dim LineList As New List(Of String)
            
            For Each line In fe
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    LineList.Add(line)
                End If
            Next
            ''Put the first one in a text box
            TextBox1.Text = LineList(0)
            'TextBox1.Text = LineList.First '(2 way to get first line using LINQ)
    
            
            ''Put the last one in a text box
            TextBox2.Text = LineList(LineList.Count - 1)
            'TextBox2.Text = LineList.Last '(2 way to get last line using LINQ)
    
            'Get the number of line that contains "SI"
            Dim NB As Integer = LineList.Count
            TextBox4.Text = NB
    
    
            Dim arrayForTheLine = Lines.Split(","c)
            'will convert the following formats date (yyyymmdd),(yyyy/mm/dd),(yyyy-mm-dd) ToShortDateString 
            Dim strDate = If(Date.TryParse(arrayForTheLine(1), Nothing), _
            CDate(arrayForTheLine(1)), CDate(New Date(Val(arrayForTheLine(1).Substring(0, 4)), Val(arrayForTheLine(1).Substring(4, 2)), Val(arrayForTheLine(1).Substring(6, 2))).ToShortDateString))
            Dim strO = arrayForTheLine(2)
            Dim strH = arrayForTheLine(3)
            Dim strL = arrayForTheLine(4)
            Dim strCl = arrayForTheLine(5)
            Dim strV = arrayForTheLine(6)
            Dim columnQuery = From line1 In Lines
                                   Let x = line1.Split(",")
                                   Where x(2) < "<o>"
                                   Select Convert.ToDouble(x(2))
    
            ' Execute and cache the results for performance.
            ' Only needed with very large files.
            Dim results = columnQuery.ToList()
    
    
            ' Perform aggregate calculations 
            ' on the column specified by col.
            Dim avgScore = Aggregate score In results Into Average(score)
            Dim minScore = Aggregate score In results Into Min(score)
            Dim maxScore = Aggregate score In results Into Max(score)
            TextBox5.Text = maxScore
            TextBox6.Text = minScore
            TextBox7.Text = avgScore
    
        End Sub

    thank you for your help



    • Edited by cat2011 Tuesday, July 17, 2012 11:13 PM
    Tuesday, July 17, 2012 9:37 PM
  • hi

    i use this code 

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    
    
            Dim Lines As String()
            Lines = File.ReadAllLines("c:\test.txt")
            Dim LineList As New List(Of String)
    
            For Each line In Lines
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    LineList.Add(line)
                End If
    
            Next
    
            ''Put the first one in a text box
            TextBox1.Text = LineList(0)
            'TextBox1.Text = LineList.First '(2 way to get first line using LINQ)
    
    
            ''Put the last one in a text box
            TextBox2.Text = LineList(LineList.Count - 1)
            'TextBox2.Text = LineList.Last '(2 way to get last line using LINQ)
    
    
            'Get the number of line that contains "SI"
            Dim NB As Integer = LineList.Count
            TextBox5.Text = NB
            
    
    
            For Each line In Lines
                If line.Contains(Comcompanyname3.SelectedItem.ToString()) Then
                    'Put all the lines that contains "SI" in a list
                    Dim arrayForTheLine = line.Split(","c)
                    Dim strO = arrayForTheLine(2)
                    Dim strH = arrayForTheLine(3)
                    Dim strL = arrayForTheLine(4)
                    Dim strCl = arrayForTheLine(5)
                    Dim strV = arrayForTheLine(6)
                End If
    
            Next
            Dim columnQuery = From line1 In Lines
                                   Let x = line1.Split(",")
                                   Where x(2) < "<o>"
                                   Select Convert.ToDouble(x(2))
    
            ' Execute and cache the results for performance.
            ' Only needed with very large files.
    
            Dim results = columnQuery.ToList()
    
    
            ' Perform aggregate calculations 
            ' on the column specified by col.
            Dim avgScore = Aggregate score In results Into Average(score)
            Dim minScore = Aggregate score In results Into Min(score)
            Dim maxScore = Aggregate score In results Into Max(score)
            TextBox4.Text = maxScore
            TextBox6.Text = minScore
            TextBox7.Text = avgScore
    
        End Sub

    but it  is return the results for the all txt file not by group as si or fa ....ETC

    thank you for your help

    Wednesday, July 18, 2012 1:12 AM