How can I create a large text file is an efficient way?

O

Oscar

I 've coded a subroutine to create a text file (in this case a xml file)
which sometimes holds a size of 500 - 900 KB. While the subroutine works
efficiently for small file size (up to 50 KB) I noted a worse efficiency in
case of large file size (up to 10 minutes of calculation time).

This is the routine :

Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlFile = fso.CreateTextFile(FilePath, True)

DO
xmlFile.Write "some text" & vbCrLf

LOOP UNTIL ..

xmlFile.Close

While debugging I've noted that the execution of line : xmlFile.Write ...
takes more and more time as the text file has grown lager.

Is there a more efficient way to create the text file ?

Regards,
Oscar
 
J

Jezebel

1. Don't use the Scripting object. The native file commands are simpler,
quicker, and more reliable.
2. Construct the file in memory then write it in one operation:

Dim pFileNum as long
Dim pFileText as string

Do
pFileText = pFileText & "sometext" & vbCr
Loop until ...

pFileNum = freefile
open FilePath for output as pFileNum
Print #pFileNum, pFileText
Close #pFileNum
 
K

Karl E. Peterson

Oscar said:
I 've coded a subroutine to create a text file (in this case a xml
file) which sometimes holds a size of 500 - 900 KB. While the
subroutine works efficiently for small file size (up to 50 KB) I
noted a worse efficiency in case of large file size (up to 10 minutes
of calculation time).

This multipost was answered, very well, elsewhere... <sigh>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top