Page 1 of 1

add FileWrite function in alpha version

Posted: 07 May 2024, 01:07
by ZhuangQu
In other languages, it's easy to write text to a file.
But in AutoHotkey, there's no direct way.
We have to write such functions by hand:

Code: Select all

FileWrite(text, path) {
    if FileExist(path)
        FileDelete(path)
    FileAppend(text, path)
}
AutoHotkey already has FileRead and FileAppend functions,
and should have FileWrite for syntactical consistency.
Alternatively, can we add an overwrite option to FileAppend?

Re: add FileWrite function in alpha version

Posted: 07 May 2024, 06:58
by lexikos

Code: Select all

FileOpen(path, "w").Write(text)

Re: add FileWrite function in alpha version

Posted: 07 May 2024, 08:15
by william_ahk
I second this, I use the above much more frequent than FileAppend. There should be a function for this.

Re: add FileWrite function in alpha version

Posted: 07 May 2024, 09:32
by ZhuangQu
lexikos wrote:
07 May 2024, 06:58

Code: Select all

FileOpen(path, "w").Write(text)
Thank you for your simple solution!
However, I advocate FileWrite for semantic consistency
- where there is FileRead and FileAppend, there should be FileWrite.
These three represent the basic operations we do with files.
It is not complete without any one.

Re: add FileWrite function in alpha version

Posted: 08 May 2024, 00:24
by just me

Code: Select all

FileWrite(Path, Text) => FileOpen(Path, "w").Write(Text)

Re: add FileWrite function in alpha version

Posted: 10 May 2024, 12:32
by pentosomes
I register an account to second this, too. I believe that every function should have its irreplaceable use. However, FileAppend is not useful because it can not overwrite.

Your two code examples of FileOpen doesn't change the situation, but makes FileAppend even less popular. My suggestion is adding an option to FileAppend, like this

Code: Select all

FileAppend(Text , Filename, "w")
where "w" means overwrite. In fact, FileAppend's options already have Encoding, RAW, 'n. One more wouldn't hurt.

Re: add FileWrite function in alpha version

Posted: 12 May 2024, 06:03
by lexikos
It can be done with a simple one-liner. Why should the interpreter and documentation be inflated with these small but completely redundant functions?

For matters of convenience, there are solutions that don't involve adding more redundant built-in functions, or waiting for someone else to do it for you.

I'm not going to consider making FileAppend do anything other than append. "Truncating and then appending" is not appending.

Re: add FileWrite function in alpha version

Posted: 12 May 2024, 08:32
by ZhuangQu
lexikos wrote:
12 May 2024, 06:03
It can be done with a simple one-liner. Why should the interpreter and documentation be inflated with these small but completely redundant functions?

For matters of convenience, there are solutions that don't involve adding more redundant built-in functions, or waiting for someone else to do it for you.

I'm not going to consider making FileAppend do anything other than append. "Truncating and then appending" is not appending.
If FileWrite is redundant, then FileRead and FileAppend are also redundant.
They can be done with a simple one-liner with FileOpen, too.
Are you considering removing them?

Re: add FileWrite function in alpha version

Posted: 12 May 2024, 16:12
by lexikos
Removing a thing which is already implemented, guaranteed by documentation and has been used for 20 years is quite different to doing nothing; i.e. not implementing a new function.

Re: add FileWrite function in alpha version

Posted: 15 May 2024, 12:21
by xMaxrayx
@ZhuangQu
Hi OP if you want middle answer you can use this method and make your additional ahk libraries folder.

https://www.autohotkey.com/docs/v2/Scripts.htm#lib


https://www.youtube.com/watch?v=nFnMiVAjkv4&list=PLQBdjw2QsRzNTsSemXv-xqcIDx9-X3vMS&index=30

then if you are using the vscode you can make auto-insert snippet have your libraries.


maybe you love this too
https://www.youtube.com/watch?v=jADuLp4-JAI