"MLH" <CR**@NorthState.net> wrote in message
news:8s********************************@4ax.com...
I use the following code blurb to read text from a file
on disk, saving it to a string var...
20 Open MyFile For Input As #1 ' Open file.
30 Do While Not EOF(1) ' Loop until end of file.
40 MyChar = Input(1, #1) ' Get one character.
50 strFileContent = strFileContent & MyChar
60 Loop
70 Close #1 ' Close file.
It is quite slow. Actually, I can read the same info
from a web server under the restrictions of DSL bandwidth 10x
faster than I can read it straight from my local drive. Are there
ways to modify my code that will speed up the process?
You can't be that surprised, can you? You are going to the hard disk,
reading a single character, adding it to a string and repeating this
read-from-disk operation thousands of times. Instead of grabbing one
character at a time, grab the lot in one go:
Public Function GetContents(strPath As String) As String
On Error GoTo Err_Handler
Dim intFile As Integer
Dim strContents As String
intFile = FreeFile
Open strPath For Binary As #intFile
strContents = String(LOF(intFile), vbNullChar)
Get intFile, , strContents
GetContents = strContents
Exit_Handler:
On Error Resume Next
Close intFile
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function