I have a logging sub routine, that writes log records to a text file. The
sub routine is copied below... The issue I have, is that this file can get
very large and as the process runs, it gets slower and slower, with the size
of the file getting very big.

Is there any way, with the scripting object, to only append to the text
file, without having to read the entire file into a variable and then append
to the variable, then write the variable back to the file.... Something like
the normal VB command of 'open for append as #1', which allows the text
record to write to the end of the file, without having to read the entire
file into memory. Is the scripting.filesystemobject, the correct object to
use, or is there another object that should be used?

This is a vbs script, but I have tried using the object browser (with the
Microsoft Scripting Runtime reference loaded) and it does not show any
'append' type method...


sub doLogging(inRecord)
dim fsoObject 'As script object
dim strText 'As string

set fsoObject = CreateObject("Scripting.FileSystemObject)

with fsoObject.OpenTextFile("c:\log.txt")
strText = .ReadAll
.Close
end with
fsoObject.DeleteFile "c:\log.txt", True

strText = strText & vbCrLf
if trim(inRecord) <> "" then strText = strText & inRecord

with fsoObject.CreateTextFile("c:\log.txt")
.Write strText
.Close
end with

set fsoObject = nothing
end sub

Re: scripting.filesystemobject - FOUND THE ANSWER.... by Ruskin

Ruskin
Mon Jul 19 16:06:30 CDT 2004

I FOUND THE ANSWER.... I have to use a stream....

Have replaced the sub routine, with the following;


sub doLogging(inRecord)

const ForReading = 1, ForWriting = 2, ForAppending = 8
const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

dim objScript 'as scripting.filesystemobject
dim objFile 'as scripting.file
dim objStream 'as scripting.textstream

set objScript = CreateObject("Scripting.FileSystemObject")
set objFile = objScript.GetFile("c:\log.txt")
set objStream = objFile.OpenAsTextStream(ForAppending,
TristateUseDefault)
objStream.Write vbCrLf & inRecord
objStream.Close
set objStream = nothing
set objFile = nothing
set objScript = nothing
end sub



"Ruskin" <ruhardie@nospam.xtra.com> wrote in message
news:EoWKc.2270$N77.202482@news.xtra.co.nz...
> I have a logging sub routine, that writes log records to a text file. The
> sub routine is copied below... The issue I have, is that this file can get
> very large and as the process runs, it gets slower and slower, with the
size
> of the file getting very big.
>
> Is there any way, with the scripting object, to only append to the text
> file, without having to read the entire file into a variable and then
append
> to the variable, then write the variable back to the file.... Something
like
> the normal VB command of 'open for append as #1', which allows the text
> record to write to the end of the file, without having to read the entire
> file into memory. Is the scripting.filesystemobject, the correct object to
> use, or is there another object that should be used?
>
> This is a vbs script, but I have tried using the object browser (with the
> Microsoft Scripting Runtime reference loaded) and it does not show any
> 'append' type method...
>
>
> sub doLogging(inRecord)
> dim fsoObject 'As script object
> dim strText 'As string
>
> set fsoObject = CreateObject("Scripting.FileSystemObject)
>
> with fsoObject.OpenTextFile("c:\log.txt")
> strText = .ReadAll
> .Close
> end with
> fsoObject.DeleteFile "c:\log.txt", True
>
> strText = strText & vbCrLf
> if trim(inRecord) <> "" then strText = strText & inRecord
>
> with fsoObject.CreateTextFile("c:\log.txt")
> .Write strText
> .Close
> end with
>
> set fsoObject = nothing
> end sub
>
>



Re: scripting.filesystemobject - FOUND THE ANSWER.... by tlavedas

tlavedas
Mon Jul 19 16:20:02 CDT 2004

Actually, the OpenTextFile works the dsame way - that is, it can also be used to open a file for append.

Tom Lavedas
===========

"Ruskin" wrote:

> I FOUND THE ANSWER.... I have to use a stream....
>
> Have replaced the sub routine, with the following;
>
>
> sub doLogging(inRecord)
>
> const ForReading = 1, ForWriting = 2, ForAppending = 8
> const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
>
> dim objScript 'as scripting.filesystemobject
> dim objFile 'as scripting.file
> dim objStream 'as scripting.textstream
>
> set objScript = CreateObject("Scripting.FileSystemObject")
> set objFile = objScript.GetFile("c:\log.txt")
> set objStream = objFile.OpenAsTextStream(ForAppending,
> TristateUseDefault)
> objStream.Write vbCrLf & inRecord
> objStream.Close
> set objStream = nothing
> set objFile = nothing
> set objScript = nothing
> end sub
>
>
>
> "Ruskin" <ruhardie@nospam.xtra.com> wrote in message
> news:EoWKc.2270$N77.202482@news.xtra.co.nz...
> > I have a logging sub routine, that writes log records to a text file. The
> > sub routine is copied below... The issue I have, is that this file can get
> > very large and as the process runs, it gets slower and slower, with the
> size
> > of the file getting very big.
> >
> > Is there any way, with the scripting object, to only append to the text
> > file, without having to read the entire file into a variable and then
> append
> > to the variable, then write the variable back to the file.... Something
> like
> > the normal VB command of 'open for append as #1', which allows the text
> > record to write to the end of the file, without having to read the entire
> > file into memory. Is the scripting.filesystemobject, the correct object to
> > use, or is there another object that should be used?
> >
> > This is a vbs script, but I have tried using the object browser (with the
> > Microsoft Scripting Runtime reference loaded) and it does not show any
> > 'append' type method...
> >
> >
> > sub doLogging(inRecord)
> > dim fsoObject 'As script object
> > dim strText 'As string
> >
> > set fsoObject = CreateObject("Scripting.FileSystemObject)
> >
> > with fsoObject.OpenTextFile("c:\log.txt")
> > strText = .ReadAll
> > .Close
> > end with
> > fsoObject.DeleteFile "c:\log.txt", True
> >
> > strText = strText & vbCrLf
> > if trim(inRecord) <> "" then strText = strText & inRecord
> >
> > with fsoObject.CreateTextFile("c:\log.txt")
> > .Write strText
> > .Close
> > end with
> >
> > set fsoObject = nothing
> > end sub
> >
> >
>
>
>