READ txt line by line from a finded value

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

READ txt line by line from a finded value

Post by sal21 »

I use this piece of code to intercept a value in txt file:

Code: Select all

....

With fsoTextStream
        Do While Not .AtEndOfStream
            LEGGI_RIGA = .ReadLine
            If Left(LEGGI_RIGA, 36) = "AP_NUMERO_INT:  " & strSearchCriteria Then
            Debug.Print "TROVATO OK!"
            CONTA = CONTA + 1
            'Exit Do
            End If
        Loop
    End With
....
now, when to code interpcet the string, in need to read only the "block" of line into the txt line by line, from the line intercepeted untill the code found:

If Left(LEGGI_RIGA, 36) = "AP_NUMERO_STOP: " & strSearchCriteria Then
debug.print end of block
end if

other way are welcome...

NOTE:
the txt contain approx 230.xxxx lines :groan: :grin: :scratch:

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

Sorry, I don't understand what you want to accomplish.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: READ txt line by line from a finded value

Post by sal21 »

HansV wrote:Sorry, I don't understand what you want to accomplish.
My code find:
If Left(LEGGI_RIGA, 36) = "AP_NUMERO_INT: " & strSearchCriteria Then
...

loop the other txt file from the line found, for example in line 4.558, end loop from line 4.558 to the code itercept the line:

If Left(LEGGI_RIGA, 36) = "AP_NUMERO_STOP: " & strSearchCriteria Then
...

in effect based var strSearchCriteria (is a index numeric) i need to intercept the head and the end of transaction.

from this loop i need to insert the lines of the lop, into a listbox.


example of txt:
.....
AP_NUMERO_INT: 1245

line
line
line
line
(this blok of lines go in ia listbox with the head and the end of transaction)
AP_NUMERO_STOP: 1245

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

Which "other text file"? :scratch:
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: READ txt line by line from a finded value

Post by sal21 »

HansV wrote:Which "other text file"? :scratch:
sorry... the same txt :grin:

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

Something like this. It will be slow, though.

Code: Select all

    Dim f As Boolean
    With fsoTextStream
        Do While Not .AtEndOfStream
            LEGGI_RIGA = .ReadLine
            If Left(LEGGI_RIGA, 36) = "AP_NUMERO_STOP:  " & strSearchCriteria Then
                Exit Do
            End If
            If f Then
                ' Add item to list box here
            End If
            If Left(LEGGI_RIGA, 36) = "AP_NUMERO_INT:  " & strSearchCriteria Then
                Debug.Print "TROVATO OK!"
                f = True
            End If
        Loop
    End With
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: READ txt line by line from a finded value

Post by sal21 »

HansV wrote:Something like this. It will be slow, though.

Code: Select all

    Dim f As Boolean
    With fsoTextStream
        Do While Not .AtEndOfStream
            LEGGI_RIGA = .ReadLine
            If Left(LEGGI_RIGA, 36) = "AP_NUMERO_STOP:  " & strSearchCriteria Then
                Exit Do
            End If
            If f Then
                ' Add item to list box here
            End If
            If Left(LEGGI_RIGA, 36) = "AP_NUMERO_INT:  " & strSearchCriteria Then
                Debug.Print "TROVATO OK!"
                f = True
            End If
        Loop
    End With

infact! very slow.... :scratch:

to find in line 231.456 the code use 15/30 secs!!!!

other way?

peraph store the lines into array?

But i have tested with 3 or 4 example on line founed and all code have error about memory :sad: :sad: :scratch: :scratch:

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

Which code caused memory errors? The code that I posted, or code to store the lines in an array?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: READ txt line by line from a finded value

Post by sal21 »

HansV wrote:Which code caused memory errors? The code that I posted, or code to store the lines in an array?
no your code....

but all my tested code to store entire txt into array and loop it!!!!

one of experiment:

Code: Select all

....
 fnum = FreeFile
    Open "c:\Moni.txt" For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)<<<< here out of memory
    Close fnum
.....

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

I fear that's unavoidable with such a large file.

I think 15-30 seconds to search a file with over 200000 lines is not so bad...
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Re: READ txt line by line from a finded value

Post by sal21 »

HansV wrote:I fear that's unavoidable with such a large file.

I think 15-30 seconds to search a file with over 200000 lines is not so bad...
OK...
but if is possible to store all line in array, and loop it?
Not is the best to loop item of array instead to read line BY line the txt?
Is possible to store the lines (approx 200.000) into array without to have prob of memory? (the dimension is approx 650Mb)

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: READ txt line by line from a finded value

Post by HansV »

I think you'll run into memory problems if you try to store the entire file into an array.
Best wishes,
Hans