Read .TXT file line by line?
Harald Staff wrote:
"Charlotte E." wrote in message
...
How to read a .TXT-file line by line?
I would like to read the first line of the .TXT-file,
Perform some action on the read data (the .TXT-line)
Read the next line - perform some action
Read the next line - perfom the action again
etc...
Until all lines of the .TXT-file is read, and the action is performed
on all lines (imported data)
My problem is that I have a HUGE .TXT-file of almost 3.000.000 lines!!!
I'm aware that going through all these lines, one-by-one, will probably
take a week or so, so I have to make my first test-program read a
smaller file, until I'm sure it is working.
But, how to read the file, line by line, in the first place, since I
can't import such a huge file in my trusty old XL2003?
Oh, I case you wonder what the 'action' is, I want to remove those
lines in the .TXT-file that doens't fullfill certain criteria, thus
(hopefully :-) ending up with a .TXT-file of only approx. 105.000
lines, which then can be converted into a webtable.
Translated from Danish as a convenience (Oversat fra dansk som en
bekvemmelighed; dersom norske, beklager, ser det samme til meg):
Hey Charlotte E.
The trick is Line Input:
Sub test()
Dim Linje As String, Avsn() As String
Dim iFnum As Integer
iFnum = FreeFile
Open "C:\Temp\Fil.Txt" For Input As #iFnum
While Not EOF(iFnum)
Line Input #iFnum, Linje
'example "some action":
Avsn = Split(Linje, " ")
Debug.Print Avsn(0)
Wend
Close #iFnum
End Sub
To clarify this one a bit, this text:
'example "some action":
Avsn = Split(Linje, " ")
Debug.Print Avsn(0)
....is where you check the line to see if it fits your criteria. To write
the lines that you want to keep to another file, do this:
Sub test()
Dim Linje As String
Dim iFnum As Integer, oFnum As Integer
iFnum = FreeFile
Open "C:\path\to\bigFile.txt" For Input As iFnum
oFnum = FreeFile
Open "output.txt" For Output As oFnum
While Not EOF(iFnum)
Line Input #iFnum, Linje
'...check criteria here...
Print #oFnum, Linje
Wend
Close #iFnum
End Sub
The method Mike S showed is *usually* faster than this way, but probably
not feasible with such a large file. (*I* wouldn't want to try it.)
Another problem is that Excel is limited in how many lines it can have in a
spreadsheet. I don't know what the limits are for 2003, but in 2000 it's
65,536 lines. If your final file has more lines than that, you'll need to
split it up.
--
Hawking! I checked the math! 2 + 2 isn't 5, it's 6! 6!!!
|