Results 1 to 9 of 9

Thread: [RESOLVED] Read certain Lines from .txt file

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Resolved [RESOLVED] Read certain Lines from .txt file

    For the moment i use this code wich is reading only one line at time:

    Code:
            Dim Tr As IO.TextReader = System.IO.File.OpenText(My.Settings.masuratori_preluare_date)
            Dim TW As System.IO.TextWriter
            Dim nume_fisier As String
    
            Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(6) & vbCrLf
    
            nume_fisier = My.Settings.masuratori_salvare_date & Format(Date.Now.Day) & "-" & Format(Date.Now.Month) & "-" & Format(Date.Now.Year) & " la " & Format(Date.Now.Hour) & "." & Format(Date.Now.Minute) & "." & Format(Date.Now.Second) & "s" & ".txt"
    
            Tr.Close()
    
            TW = System.IO.File.CreateText(nume_fisier)
            TW.WriteLine(MyFileLine)
            'Write another line 
            'TW.WriteLine("Hello World Again")
            TW.Flush()
            TW.Close()
    How can i do if i want to read more than one specific line ?!

    Thanks in advice!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Read certain Lines from .txt file

    You're reading the file and splitting it into lines, but then you only get one of those lines and throw the rest away. How about you keep all the lines. Then you can access any one you want any time. There's a far more elegant way to get the lines too:
    vb.net Code:
    1. Dim lines As String() = IO.File.ReadAllLines("file path here")
    Now you can access any line you want from the 'lines' array at your leisure.

    EDIT: Thanks stanav, but I might have to rep you down for finding fault in my post.
    Last edited by jmcilhinney; Mar 14th, 2008 at 09:09 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Read certain Lines from .txt file

    Just a minor correction on JMC's code. It should be this
    Code:
    Dim lines() As String = IO.File.ReadAllLines("file path here")
    
    'Or this
    
    Dim lines As String() = IO.File.ReadAllLines("file path here")

  4. #4

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: Read certain Lines from .txt file

    I have put your code but in the text file is showing me "System.String[]"

    Code:
            Dim Tr As IO.TextReader = System.IO.File.OpenText(My.Settings.masuratori_preluare_date)
            Dim TW As System.IO.TextWriter
            Dim nume_fisier As String
    
            'Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(1) & vbCrLf
    
            Dim MyFileLines As String() = IO.File.ReadAllLines(My.Settings.masuratori_preluare_date)
    
            nume_fisier = My.Settings.masuratori_salvare_date & Format(Date.Now.Day) & "-" & Format(Date.Now.Month) & "-" & Format(Date.Now.Year) & " la " & Format(Date.Now.Hour) & "." & Format(Date.Now.Minute) & "." & Format(Date.Now.Second) & "s" & ".txt"
    
            Tr.Close()
    
            TW = System.IO.File.CreateText(nume_fisier)
            TW.WriteLine(MyFileLines)
            TW.Flush()
            TW.Close()
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  5. #5
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: Read certain Lines from .txt file

    You have to join the array before you can write it to the file.
    vb.net Code:
    1. TW.WriteLine(String.Join(ControlChars.CrLf, MyFileLines))

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  6. #6

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: Read certain Lines from .txt file

    But my problem is still not solved!

    How can i read for example only line 4 and 6 from that text file ?

    Thanks again for your big help!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Read certain Lines from .txt file

    We showed you how to read the contents of a file into a string array where each element holds a line of your text. Now if you want to read a specific line, you just read it from the array at a specific index. Arrays are zero based, so for example, if you want line#3 in your text file, you will read the array element at index#2.
    Code:
     
    Dim lines As String() = IO.File.ReadAllLines("path here")
    Dim line3 As String = lines(2)
    One thing you have to keep in mind that if you're trying to access an element that is not in the array, you program will crap out.

  8. #8

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: Read certain Lines from .txt file

    Working...

    Thanks alot for your big help!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  9. #9
    New Member
    Join Date
    Mar 2011
    Posts
    1

    Re: [RESOLVED] Read certain Lines from .txt file

    thank you all guys for helping me also.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width