Sign in to follow this  
Followers 0
leuce

How to overwrite a file

7 posts in this topic

G'day everyone

I know that I can open a file with FileOpen, and that I can append to that file using FileWrite, but how do I overwrite an existing file with a new, empty file, so that whatever I write to it, is the only thing that is written to it? One method might be to tell AutoIt to delete the original file before executing a FileOpen (which will then create the file), but is there a way to simply create a file and save it and not get an error message even though the file already exists?

Thanks

Samuel

PS There's no code yet, but I'll post it when I write it.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

FileOpen ( "filename", mode )

Parameters

filename Filename of the text file to open.

mode Mode (read or write) to open the file in.

0 = Read mode

1 = Write mode (append to end of file)

2 = Write mode (erase previous contents) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Both write modes will create the file if it does not already exist.

not quite what you want

Edited by pecloe

Share this post


Link to post
Share on other sites

Both write modes will create the file if it does not already exist.

Let me explain. There is already a file named Foo.txt in the folder. I want AutoIt to create a new file called Foo.txt and overwrite the existing file Foo.txt without asking. Or to put it a different way: I want AutoIt to open the file Foo.txt and overwrite (not append) the existing contents with new content.

Share this post


Link to post
Share on other sites

" 2 = Write mode (erase previous contents) "

since foo.txt already exists mode 2 will overwrite without a prompt (at least in production)

Share this post


Link to post
Share on other sites

" 2 = Write mode (erase previous contents) "

Thanks... I didn't see your <<<<<<<<<< in the previous post.

Share this post


Link to post
Share on other sites

lots of times i just delete the previous file or create a backup copy

FileDelete(@ScriptDir & "\Address.bak")
FileCopy(@ScriptDir & "\Address.ini, @ScriptDir & "\Address.bak", 1)
FileDelete(@ScriptDir & "\Address.ini")
FileWriteLine(@ScriptDir & "\Address.ini, "[Address]")

8)


NEWHeader1.png

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