File Input Question
As far as I know Tim's spot on, in that
(a) there's no way as far as I know - unless you start to get into
random access files;
and
(b) you will be amazed at how blindingly fast VBA can search through
thousands of files, parse each line, compare strings. It's amazing.
However, if you need to access specific points in a file... I could
understand why this might sound a good idea -- say you want to go to
line 9999, it *might* be quicker to read the file in one fell swoop
rather than bit by bit. (But surely - definitely - not -as Tim wrote -
if you need to just access data at the beginning of the file).
To do this maybe something like:
Sub ReadWholeFile()
Dim myFile As String
Dim myLines() As String
Dim hFile As Integer
hFile = FreeFile
Open "c:\test.txt" For Input As #hFile
myFile = Input(LOF(hFile), hFile)
Close #hFile
'If file has records of identical length (say 100)
'you could access line 999 thus:
myrecordlength = 100 + Len(vbCrLf)
Line999 = Mid$(myFile, myrecordlength * 999, myrecordlength)
'Alternatively, you could split everything up by line into an
'array
myLines = Split(myFile, vbCrLf)
'You can now address line 999 thus:
Line999 = myLines(999)
End Sub
I have no idea if this will be quicker or (massively) slower than
reading a large file piecemeal. I would be interested to know if you do
a performance test!
HTH,
G
Tim Williams wrote:
I don't think you can "go directly" without having to count lines: in any
case it doesn't take long to count to 3...
How large are the files? Even if you checked them all it wouldn't take too
long (unless of course this is something you need to do every minute). For
a one-time job even a couple of minutes should be acceptable.
Tim.
|