importing file contents into a variable
Hi all.
I've been using this code for years to get a file's contents into a variable:
Open strFile For Input As #1
strX = ""
Do Until EOF(1) = True
Line Input #1, x
strX = strX & x
Loop
Close #1
Is there a faster way?
I'm moving a lot of files in a loop, so every second counts.
Thanks.
Re: importing file contents into a variable
I guess I forgot about this:
strX = Input(LOF(1), #1)
anything faster than this?
Re: importing file contents into a variable
also,
for downloading html files,
anything faster than this:
Public Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Public Function DownloadFile(URL, LocalFilename) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
Re: importing file contents into a variable
To my knowledge, this is the fastest possible way:
Code:
Dim MyData As String
Open "MyTextFile.Txt" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
MyData now contains all of the file as a string variable.
Re: importing file contents into a variable
Quote:
Originally Posted by
wengang
I'm moving a lot of files in a loop, so every second counts.
Moving files? As in reading a file, writing the data (unchanged) to a new file, then deleting the old file? If so, try the Name statement:
Name oldpathname As newpathname
Re: importing file contents into a variable