|
-
Jul 23rd, 2011, 05:09 AM
#1
Thread Starter
PowerPoster
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.
-
Jul 23rd, 2011, 06:31 AM
#2
Re: GOTo teh certain line in txt file
jump means? do u need to read the 27th line?
-
Jul 23rd, 2011, 07:05 AM
#3
Lively Member
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.
-
Jul 23rd, 2011, 10:10 AM
#4
Thread Starter
PowerPoster
Re: GOTo teh certain line in txt file
 Originally Posted by seenu_1st
jump means? do u need to read the 27th line?
yes! cont 27th lines after VAI:, in this case.
-
Jul 23rd, 2011, 10:18 AM
#5
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.
-
Jul 23rd, 2011, 10:18 AM
#6
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
-
Jul 23rd, 2011, 10:24 AM
#7
Thread Starter
PowerPoster
Re: GOTo teh certain line in txt file
 Originally Posted by seenu_1st
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!!!
-
Jul 23rd, 2011, 10:41 AM
#8
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...
-
Jul 23rd, 2011, 03:00 PM
#9
Thread Starter
PowerPoster
Re: GOTo teh certain line in txt file
 Originally Posted by seenu_1st
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...
-
Jul 23rd, 2011, 08:05 PM
#10
Re: GOTo teh certain line in txt file
this thread may be useful for u.
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
|