Results 1 to 8 of 8

Thread: txt file: getting data from the specified line number

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2004
    Location
    Turkey, Eskisehir
    Posts
    37

    txt file: getting data from the specified line number

    Hi,
    as using at simple database action, I searched the forum and the net, but I couldn't find the exact answer of the question: Can we get the data which we want from the txt files? for ex. I need the data on line 5.
    have a nice day...

  2. #2
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: txt file: getting data from the specified line number

    One simple way would be to open the file and split it into an array using VbCrLf as the delimiter. Then, the line you need -1 (Because arrays are 0-based). So with this method, line 5, would be the 4th element in the array.

  3. #3
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: txt file: getting data from the specified line number

    This code will work too. It reads the file line by line and exits the loop after the 5th line which it shows in a MsgBox.

    Code:
        Dim fs
        Dim ts
        Dim line As String
        Dim filepath As String
        filepath = App.Path & "\test.txt"
        Dim i As Long
        i = 1
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(filepath, 1, False)
        Do While Not ts.AtEndOfStream
            line = ts.readline
            If i = 5 Then
                Exit Do
            End If
            i = i + 1
        Loop
        MsgBox line
    Let me know if it helps

  4. #4
    Member
    Join Date
    Mar 2007
    Posts
    46

    Re: txt file: getting data from the specified line number

    You can get all the text out and into an array; you can start the array at 1; you can omit blank lines....

    Code:
    Dim sLine As String
    Dim iFileNo As Integer
    Dim a()
    Dim c As Integer
    
    ReDim a(0)
    c = 1
    
    iFileNo = FreeFile
    'open the file for reading
    Open "C:\Test.txt" For Input As #iFileNo
    
    'read the file until we reach the end
    Do While Not EOF(iFileNo)
        Input #iFileNo, sLine
        'If sLine <> "" then      <-- This will allow you to skip blank lines
        ReDim Preserve a(c)
        a(c) = sLine
        c = c + 1
        'endif
    Loop
    
    'close the file
    Close #iFileNo
    You can then get whatever line you want out of the array using the line number, so for line 5 use: a(5)

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2004
    Location
    Turkey, Eskisehir
    Posts
    37

    Re: txt file: getting data from the specified line number

    hi, sorry for I am late,
    I was thinking use the txt file for my database app. which I previously used the excel that relativey slow.
    In the weekend I tried on it and succeded. the lines consists of:
    abc*dfg*fgh*dsa*....
    I chosed "*" as the determining character of the elements of the array.
    problem: when there is "," in the string statement, I can not take the strings after the coma. and the program gives error.
    not: I will ask this problem as another quest!
    thank you and have a nice day...

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2004
    Location
    Turkey, Eskisehir
    Posts
    37

    Re: txt file: getting data from the specified line number

    addition: the code part I used:
    dim s() as string
    ....
    s=split(state,"*",20)
    .....

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: txt file: getting data from the specified line number

    I guess your problem with "," is that you are only reading the first line of the file, and the code understands the "," as a lineending sign!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2004
    Location
    Turkey, Eskisehir
    Posts
    37

    Re: txt file: getting data from the specified line number

    Quote Originally Posted by opus
    I guess your problem with "," is that you are only reading the first line of the file, and the code understands the "," as a lineending sign!
    hi, I wasn't know the "," sign is lineending mark !
    in net, I meet "line input" and just I tried it and I guess It works.

    apologise: I had to searched net first
    excuse: I hadn't thought that I could find the answers in the net

    have a nice day and excuse me...

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