Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read only a part of txt file

Status
Not open for further replies.

2009luca

Programmer
Jul 27, 2013
222
0
16
IT
I already know the number of the first line to read and the last one.

without looping whole file line by line!

how to loop only the lines from the number of line 10 to the number line 100?

 
You will have to read the file in some way.
Easiest (using FileSystemObject):
Code:
For i = 1 to 9
   if objTextFile.AtEndOfStream Exit For
   objTextFile.ReadLine
Next i
By reading 89 lines but doing nothing, you sort of "skip" over these lines. Now you can read the next 90 lines, then quit after line 100.

Another option would be (provided the file is not very big): Read the entire contents, split by CrLf, loop over indexes 9 through 99 of the resulting array.

See also: [faq333-504]

Would that be good enough?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
>provided the file is not very big

And how might we characterise "not very big"?
 
strongm said:
And how might we characterise "not very big"?
Maybe a file in the kB range rather than MB, I guess?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
im sorry...
really the txt file can have approx 2.500.XXX lines, and i need to get line by line from line number 589.458 to 600.000...
 
> the kB range

VB (and the Split function) happily handles much bigger text files than that.

>rather than MB

10s of megabytes will work quite happily. Indeed, I've certainly used your suggested solution in the past (read the entire contents, split by CrLf) with text files well over 100Mb in size
 
strongm said:
I've certainly used your suggested solution in the past (read the entire contents, split by CrLf) with text files well over 100Mb in size

Wow! I will try to remember this rough range then, thanks!

2009luca said:
the txt file can have approx 2.500.XXX lines, and i need to get line by line from line number 589.458 to 600.000...

In that case... [ponder] ... is there any way you could access the original data source that produced this file rather than the txt file? Or: Is the txt of a specific delimited sort so that you could use it as a data source, querying the desired lines via an OLEDB connection?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I would start simply this way:

Code:
Dim strTextLine As String
Dim L As long

Open [red]"C:\Temp\MyTextFile.txt"[/red] For Input As #1
Do While Not EOF(1)
    L = L + 1
    Line Input #1, strTextLine   
    If L > [blue]589457[/blue] Then[green]
        'Do your magic here[/green]
    End If
    If L = [blue]600000[/blue] Then
        Exit Loop
    End If
Loop
Close #1


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top