|
-
Jan 5th, 2008, 01:40 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Read A Specific Line From Big Text File
How can I read specific line from big *.TXT file?
For example *.TXT file size is 200MB and I want read specific line from this file how can I do that ?
-
Jan 5th, 2008, 02:17 PM
#2
Re: Read A Specific Line From Big Text File
This won't be most effcient way but it may work for you:
Code:
Private Sub Command1_Click()
Dim sText As String
Dim arLines() As String '<<< declare array at the form level if you need to keep it
Open App.Path & "\sample.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
arLines() = Split(sText, vbNewLine)
sText = ""
Debug.Print arLines(5)
'erase array if you don't need it
'''Erase arLines()
End Sub
edit:
NOTE: if line number isn't available then you need to loop through file (or array) to get a match on specific text.
Code:
Private Sub Command1_Click()
Dim sText As String
Dim arLines() As String '<<< declare array at the form level if you need to keep it
Dim arResults() As String
Dim i As Long
Open App.Path & "\sample.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
arLines() = Split(sText, vbNewLine)
sText = ""
arResults() = Filter(arLines(), "text to search", True, vbTextCompare)
For i = 0 To UBound(arResults)
Debug.Print arResults(i)
Next i
'erase both arrays if necessary
'''Erase arLines()
'''Erase arResults()
End Sub
Last edited by RhinoBull; Jan 5th, 2008 at 02:42 PM.
-
Jan 5th, 2008, 02:18 PM
#3
Re: Read A Specific Line From Big Text File
And where is this line, do you know what line number? Is the text file formatted in a specific way that each line is x nr of characters? More specific details may help. Otherwise, you may have to read line by line until you find it.
-
Jan 5th, 2008, 09:30 PM
#4
Re: Read A Specific Line From Big Text File
thnks for that rhino, i didn't know about filtering arrays, always something to learn
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 5th, 2008, 09:41 PM
#5
Re: Read A Specific Line From Big Text File
 Originally Posted by RhinoBull
This won't be most effcient way but it may work for you...
The Filter function is new to me too so nice one, but if the text file is 200MB won't the peak ram use be over 800MB! (arLines() = Split(sText, vbNewLine))
-
Jan 5th, 2008, 10:07 PM
#6
Re: Read A Specific Line From Big Text File
 Originally Posted by Milk
...but if the text file is 200MB won't the peak ram use be over 800MB! (arLines() = Split(sText, vbNewLine))
If you have a better solution you're welcome to post it.
-
Jan 5th, 2008, 10:11 PM
#7
Re: Read A Specific Line From Big Text File
 Originally Posted by RhinoBull
If you have a better solution you're welcome to post it.
If memory is an issue, a Do Loop, using Line Input til EOF or line found would work too.
-
Jan 5th, 2008, 10:13 PM
#8
Re: Read A Specific Line From Big Text File
 Originally Posted by westconn1
thnks for that rhino, i didn't know about filtering arrays, always something to learn
You're welcome. There is a thread in FAQ I created some time ago - link is in my signature.
-
Jan 5th, 2008, 10:33 PM
#9
Re: Read A Specific Line From Big Text File
 Originally Posted by LaVolpe
If memory is an issue, a Do Loop, using Line Input til EOF or line found would work too.
But loop will eat up your cpu so I'm not sure what's better (or worst for that matter) and formula is quite simple:
VB6 + large files <> Efficiency
-
Jan 5th, 2008, 11:40 PM
#10
Re: Read A Specific Line From Big Text File
Okay so this is not so pretty, but it's very fast...
Edit: Just a minor tweak, the buffer size has been reduced to &H10000 (64KB), and is now passed as an optional argument. Testing on my PC showed that that size is optimal. LineNumber is passed Byref and returns the file position of the line. With a 50MB text file, searching for the 337'227th (last) line, the function was returning in around 250 milliseconds.
Code:
Private Function GetSpecificLine(filename As String, ByRef LineNumber As Long, Optional BufferSize As Long = &H10000) As String
Dim FF As Integer, Buf() As Byte, LnCt As Long, Pos As Long, i As Long, SLn As Long, ELn As Long
'Linenumber is passed Byref and returns the file position of the line
If BufferSize < 1 Then Exit Function
ReDim Buf(BufferSize - 1)
LnCt = 1
Pos = 1
LineNumber = Abs(LineNumber) - 1
If LineNumber < 1 Then SLn = 1
On Error GoTo EH
FF = FreeFile
Open filename For Binary As #FF
For Pos = 1 To LOF(FF) Step BufferSize
Get #FF, Pos, Buf
For i = 0 To BufferSize - 1
If Buf(i) = 13 Then
LnCt = LnCt + 1
If LnCt > LineNumber Then
If SLn Then
ELn = Pos + i - 1
Exit For
Else
SLn = Pos + i + 2
End If
End If
End If
Next i
If i <> BufferSize Then Exit For 'the line has been found
Next Pos
If SLn Then
LineNumber = SLn
If ELn = 0 Then ELn = LOF(FF)
ReDim Buf(ELn - SLn)
Get #FF, SLn, Buf
GetSpecificLine = StrConv(Buf, vbUnicode)
End If
Close #FF
Exit Function
EH:
'tell user of file error here
End Function
Last edited by Milk; Jan 6th, 2008 at 06:58 AM.
-
Jan 6th, 2008, 09:03 AM
#11
Thread Starter
Hyperactive Member
Re: Read A Specific Line From Big Text File
WOW! Thanks for all
Nice code Milk and realy work very fast!
But can you explain me more about "BufferSize As Long=65536"
([filename As String,LineNumber As Long,BufferSize As Long=65536])
What optimal BufferSize I have to choose?
-
Jan 6th, 2008, 07:17 PM
#12
Re: Read A Specific Line From Big Text File
Another option is to import the data into a database.
-
Mar 23rd, 2015, 02:56 AM
#13
PowerPoster
Re: Read A Specific Line From Big Text File
 Originally Posted by Milk
Okay so this is not so pretty, but it's very fast...
Edit: Just a minor tweak, the buffer size has been reduced to &H10000 (64KB), and is now passed as an optional argument. Testing on my PC showed that that size is optimal. LineNumber is passed Byref and returns the file position of the line. With a 50MB text file, searching for the 337'227th (last) line, the function was returning in around 250 milliseconds.
Code:
Private Function GetSpecificLine(filename As String, ByRef LineNumber As Long, Optional BufferSize As Long = &H10000) As String
Dim FF As Integer, Buf() As Byte, LnCt As Long, Pos As Long, i As Long, SLn As Long, ELn As Long
'Linenumber is passed Byref and returns the file position of the line
If BufferSize < 1 Then Exit Function
ReDim Buf(BufferSize - 1)
LnCt = 1
Pos = 1
LineNumber = Abs(LineNumber) - 1
If LineNumber < 1 Then SLn = 1
On Error GoTo EH
FF = FreeFile
Open filename For Binary As #FF
For Pos = 1 To LOF(FF) Step BufferSize
Get #FF, Pos, Buf
For i = 0 To BufferSize - 1
If Buf(i) = 13 Then
LnCt = LnCt + 1
If LnCt > LineNumber Then
If SLn Then
ELn = Pos + i - 1
Exit For
Else
SLn = Pos + i + 2
End If
End If
End If
Next i
If i <> BufferSize Then Exit For 'the line has been found
Next Pos
If SLn Then
LineNumber = SLn
If ELn = 0 Then ELn = LOF(FF)
ReDim Buf(ELn - SLn)
Get #FF, SLn, Buf
GetSpecificLine = StrConv(Buf, vbUnicode)
End If
Close #FF
Exit Function
EH:
'tell user of file error here
End Function
hI Milk ...
Sorry if i post on old question.
Admit my line is the number 457, how to loop the rest of txt file to the end, from this line?
note:
Tested the code, is a lightning and work perfect!
-
Mar 23rd, 2015, 05:02 AM
#14
Re: [RESOLVED] Read A Specific Line From Big Text File
Very basic sample:
Code:
Dim fID As Integer
Dim sLine As String
Dim lLineCnt As Long
fID = FreeFile
Open "PathToYourFile" For Input As #fID
Do Until EOF(fID)
Line Input #fID, sLine
lLineCnt = lLineCnt + 1
If lLineCnt >= 457 Then
' Process the data
End If
Loop
Close #fID
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|