Hobbyist

FileWrite and FileOpen

4 posts in this topic

I have a question about FileWrite out of the Help section.
 
Its says:
 
FileWrite ( "filehandle/filename", "text/data" ) and says
If a filename is given rather than a file handle, the file will be opened and closed during the function call.
And under FileOpen ( "filename" [, mode = 0] )
 
So if I am using "filehandle", I don't have to FileOpen? And in that case how do I indicate append or over write?
If I read this correctly I only need to do that when using "filename".
 
 
Thanks for any guidance.
Hobbyist

Share this post


Link to post
Share on other sites



FileOpen returns a handle to the file. The file later needs to be closed using FileClose.

You can use FileWrite by providing either this handle or the filename.

If the filename is used then FileOpen/FileClose is being done implicitely by FileWrite and hence can be omitted.

If you need to write to a file more than once then use FileOpen/FileWrite/FileClose for performance reasons.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@

water

 

Thanks for the quick response.

That makes me think of another question (as I am thinking this through before I start lopping in code).

Going with your  line of direction, I should really do a FileOpen(and in my case assign a file over write mode) outside any loop of writing?

FileOpen(file, mode)

for $i = x to y

do some stuff

FileWrite(file, data @CRLF)

next

Fileclose.

What I gather is, in using the mode of overwrite, the existing data in the file is deleted on the first pass?  And therefore I only need delete it once, thus my FileOpen outside the loop. Otherwise i would be deleting my previously written record except for the last one "y" if Open was inside the loop.

Thanks again.

Share this post


Link to post
Share on other sites

Correct. FileOpen and FileClose should be outside the loop. When you set mode to $FO_OVERWRITE then the file is deleted and all new records are appended.

Your example should work just fine.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now