Results 1 to 10 of 10

Thread: GOTo teh certain line in txt file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    GOTo teh certain line in txt file

    i use this code to loop line by line a txt file:

    Option Explicit
    Sub ReadFileLineByLine()

    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
    Const TristateUseDefault = -2
    Const TristateTrue = -1
    Const TristateFalse = 0

    Dim oFS As Object
    Dim oFile As Object
    Dim oStream As Object
    Dim sFilePathAndName As String, sRecord As String, TEST As String
    Dim RIGA As Long, ROW As Long

    sFilePathAndName = "e:\temp\TEST.txt"


    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.GetFile(sFilePathAndName)
    Set oStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)

    Do While Not oStream.AtEndOfStream
    ROW = oStream.Line
    If Trim(Len(oStream.ReadLine) > 0) Then
    RIGA = RIGA + 1
    If Mid(sRecord, 1, 4) = "VIA:" Then
    'oStream.Skip (0)
    sRecord = oStream.ReadLine
    Debug.Print sRecord
    End If

    sRecord = oStream.ReadLine
    TEST = sRecord
    End If
    Loop

    oStream.Close

    End Sub


    now i in text file are present various not important lines (are 26 lines between VIA: and ESTFINE)

    VAI:
    ...
    ...
    ESTFINE

    Note:
    1)this blok of line are present il all part of txt with the same structure and position of string
    2) i have approx 230.000 lines to read


    my question is:
    is possible to jump directlly to the 27Th line without to read not important this lines?
    Last edited by luca90; Jul 23rd, 2011 at 10:17 AM.

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: GOTo teh certain line in txt file

    jump means? do u need to read the 27th line?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: GOTo teh certain line in txt file

    Try This:

    Code:
    Public Function ReadFile(TxtFileName As String, LineNum As Long) As String
    If LineNum <= 0 Then
        ReadFile = "Invalid Line Number!" & vbNewLine & vbNewLine & "Line Number Must be greater than 0 "
        MsgBox ReadFile
        Exit Function
    End If
    
    Dim lcount As Long
    lcount = 0
    Dim TextLine
        Open TxtFileName For Input As #1
            Do While Not EOF(1)
                Line Input #1, TextLine
                lcount = lcount + 1
            Loop
        Close #1
    If LineNum <= lcount Then
    lcount = 0
        Open TxtFileName For Input As #1
        Do While Not EOF(1)
                Line Input #1, TextLine
                lcount = lcount + 1
            If lcount = LineNum Then
                Close #1
                Exit Do
            Else
                TextLine = ""
            End If
        Loop
    Else
        ReadFile = "Invalid Line Number! " & vbNewLine & vbNewLine & "Line number must be <= Total Lines in the file!"
        MsgBox ReadFile
        Exit Function
    End If
    ReadFile = TextLine
    End Function
    Last edited by Stupidiot; Jul 23rd, 2011 at 07:09 AM.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    Re: GOTo teh certain line in txt file

    Quote Originally Posted by seenu_1st View Post
    jump means? do u need to read the 27th line?
    yes! cont 27th lines after VAI:, in this case.

  5. #5
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: GOTo teh certain line in txt file

    1. load the file into a string like this,
    Code:
    SomeString = Input(LOF(1), #1)
    2. split the string using Split() function, delimiter is vbCrLf, it wil become an array
    3. once u get the array, loop thru array Lbound to Ubound to find VAI using InStr() function, once u get the line number of VAI, u can get the 27th(from VAI) string in array.
    Last edited by seenu_1st; Jul 23rd, 2011 at 10:21 AM.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: GOTo teh certain line in txt file

    Than answer is that you cannot just jump to a line in a text file (unless you knew in advance the length of every line in bytes). You'll want to read up to the desired line, ignoring those lines read, then read & process the desired line. Other options include reading the entire file and split it by using a delimiter of carriage returns (if they exist) and then access array members of that split text file, as needed.

    Edited: seenu_1st and I posted at same time & both offered alternative solution
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    Re: GOTo teh certain line in txt file

    Quote Originally Posted by seenu_1st View Post
    1. load the file into a string like this,
    Code:
    SomeString = Input(LOF(1), #1)
    2. split the string using Split() function, delimiter is vbCrLf, it wil become an array
    3. once u get the array, loop thru array Lbound to Ubound to find VAI using InStr() function, once u get the line number of VAI, u can get the 27th(from VAI) string in array.
    yes i know the way to read entire file in memory... but in thi s case the line are 230.000 approx in other case i can have 2.400.000 of lines!
    And have prob with overflow!!!

  8. #8
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: GOTo teh certain line in txt file

    this is the way i did now for u,
    Code:
    Private Sub Command1_Click()
    Dim StrVal As String, StrSplit() As String
    Dim i As Integer
    Open Path For Input As #1
        StrVal = Input(LOF(1), #1)
    Close #1
    StrSplit = Split(StrVal, vbCrLf)
    For i = LBound(StrSplit) To UBound(StrSplit)
        If InStr(1, StrSplit(i), "VAI") > 0 Then
            Exit For
        End If
    Next
    MsgBox StrSplit(i + 27) '27th line from VAI
    End Sub
    but as u told the file size is too big, hav to think...
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,943

    Re: GOTo teh certain line in txt file

    Quote Originally Posted by seenu_1st View Post
    this is the way i did now for u,
    Code:
    Private Sub Command1_Click()
    Dim StrVal As String, StrSplit() As String
    Dim i As Integer
    Open Path For Input As #1
        StrVal = Input(LOF(1), #1)
    Close #1
    StrSplit = Split(StrVal, vbCrLf)
    For i = LBound(StrSplit) To UBound(StrSplit)
        If InStr(1, StrSplit(i), "VAI") > 0 Then
            Exit For
        End If
    Next
    MsgBox StrSplit(i + 27) '27th line from VAI
    End Sub
    but as u told the file size is too big, hav to think...
    tKX for suggestion but error 14 in StrVal = Input(LOF(1), #1)

    in my last reply i have insert a note about the possible numbers of lines in txt file...

  10. #10
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: GOTo teh certain line in txt file

    this thread may be useful for u.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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