add FileWrite function in alpha version

Propose new features and changes
ZhuangQu
Posts: 33
Joined: 06 Feb 2022, 20:54

add FileWrite function in alpha version

Post by ZhuangQu » 07 May 2024, 01:07

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?

lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: add FileWrite function in alpha version

Post by lexikos » 07 May 2024, 06:58

Code: Select all

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

william_ahk
Posts: 500
Joined: 03 Dec 2018, 20:02

Re: add FileWrite function in alpha version

Post by william_ahk » 07 May 2024, 08:15

I second this, I use the above much more frequent than FileAppend. There should be a function for this.

ZhuangQu
Posts: 33
Joined: 06 Feb 2022, 20:54

Re: add FileWrite function in alpha version

Post by ZhuangQu » 07 May 2024, 09:32

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.
Last edited by ZhuangQu on 10 May 2024, 04:11, edited 1 time in total.

just me
Posts: 9520
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: add FileWrite function in alpha version

Post by just me » 08 May 2024, 00:24

Code: Select all

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

pentosomes
Posts: 1
Joined: 10 May 2024, 12:15

Re: add FileWrite function in alpha version

Post by pentosomes » 10 May 2024, 12:32

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.

lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: add FileWrite function in alpha version

Post by lexikos » 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.

ZhuangQu
Posts: 33
Joined: 06 Feb 2022, 20:54

Re: add FileWrite function in alpha version

Post by ZhuangQu » 12 May 2024, 08:32

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?

lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: add FileWrite function in alpha version

Post by lexikos » 12 May 2024, 16:12

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.

User avatar
xMaxrayx
Posts: 201
Joined: 06 Dec 2022, 02:56
Contact:

Re: add FileWrite function in alpha version

Post by xMaxrayx » 15 May 2024, 12:21

@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
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Post Reply

Return to “Wish List”