Sign in to follow this  
Followers 0
computergroove

How do I delete the contents of a file or make and write to a new one?

5 posts in this topic

I figured out how to write to an existing file but I cannot figure out how to erase the contents of the file before I write to it. Deleting the file and recreating a new one would work too but I cant find info on either.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

From the help file:

FileOpen ("filename" [,mode])

Opens a text file for reading or writing.

Parameters

filename Filename of the text file to open.

mode [optional] Mode to open the file in.

Can be a combination of the following:

0 = Read mode (default)

1 = Write mode (append to end of file)

2 = Write mode (erase previous contents)

8 = Create directory structure if it doesn't exist (See Remarks).

16 = Force binary mode (See Remarks).

32 = Use Unicode UTF16 Little Endian reading and writing mode. Reading does not override existing BOM.

64 = Use Unicode UTF16 Big Endian reading and writing mode. Reading does not override existing BOM.

128 = Use Unicode UTF8 (with BOM) reading and writing mode. Reading does not override existing BOM.

256 = Use Unicode UTF8 (without BOM) reading and writing mode.

16384 = When opening for reading and no BOM is present, use full file UTF8 detection. If this is not

used then only the initial part of the file is checked for UTF8.

The folder path must already exist (except using mode '8' - See Remarks).

What you want is mode 2

FileOpen("Readme.txt", 2)  ;erase preivious contents
Edited by Varian

Share this post


Link to post
Share on other sites

Well surely Varian opinion is more professional than mine, but, i think also FileRead would be fine.

You can read the file with it, copy where you need it, then delete the content of the previous with filedelete.

FileReadLine allow a selective reading.


I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Share this post


Link to post
Share on other sites

Well surely Varian opinion is more professional than mine, but, i think also FileRead would be fine.

You can read the file with it, copy where you need it, then delete the content of the previous with filedelete.

FileReadLine allow a selective reading.

You answered with the OP's other option, so your point is just as valid. I would suggest the FileOpen() mode 2 depending on the way in which the OP chooses/needs to create the "replacement" file. It may be that he/she would still utilize FileOpen() to create this file, or maybe _FileCreate() because unless your function or command creates the file intrinsically, he/she would still need to create the file again in order to write to it.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That worked. Thank You. Also I was checking the manual for filewrite and filereplace. I never would have guessed fileopen.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

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
Sign in to follow this  
Followers 0