this post was submitted on 13 Apr 2025
6 points (100.0% liked)

AutoHotkey

179 readers
1 users here now

founded 2 years ago
MODERATORS
 

For example, can FileAppend() be set to create something like %COMPUTERNAME%test.txt? I have no idea of what the code for this would be like, if so.

top 8 comments
sorted by: hot top controversial new old
[–] SpikesOtherDog@ani.social 2 points 4 days ago (1 children)

Yes, computer name and date are both easily pulled by ahk.

https://www.autohotkey.com/docs/v1/Variables.htm

[–] Flagstaff@programming.dev 1 points 3 days ago (1 children)

Right, thanks (also v2 so that'd be https://www.autohotkey.com/docs/v2/Variables.htm), but I'm wondering about the syntax to actually use A_ComputerName when the directory is involved. I don't know how to dynamically include this variable inside the filename of a new .TXT that AutoHotkey would create.

[–] SpikesOtherDog@ani.social 1 points 3 days ago (1 children)

You would either concatenate it or use the format command. That is explained at the beginning of the document. You could assign the filename to a variable.

filename := A_ComputerName. ".txt"

I am on mobile so it is much easier to link to examples. What have you tried?

[–] Flagstaff@programming.dev 1 points 3 days ago* (last edited 3 days ago) (1 children)

I'm also currently on mobile and while I know about concatenation, I don't understand how to incorporate the filename into the path. FileAppend() requires the path if you want it elsewhere than the script's folder (which I do), so how would that work? "C:\" . filename?

It's currently FileAppend('test', 'C:\doc.txt'), for example.

[–] SpikesOtherDog@ani.social 2 points 3 days ago (1 children)

FileAppend is to write data to the end of a file.

If your computer name is Alice, then

FileAppend A_ComputerName, "Helloworld.txt"

This changes the text of HelloWorld.Txt to

"Hello World.

Alice"

Where Helloworld.txt will (probably) be in the same folder as your script.

You are asking about the file name, which would be

FileAppend "asdf", A_Computername . " Helloworld.txt"

Filename:

"Alice Helloworld.txt"

Text:

"Hello World

asdf"

FileAppend does create a file, so you can just start calling the file and appending to it with the same command:

MyData := "GIGOasdfasdfasdfasdf" MyPath:= "C:\OutputFolder" . A_Computername

FileAppend MyData, MyPath, `n

[–] Flagstaff@programming.dev 1 points 1 day ago

Gotcha, I don't know why my brain couldn't figure this out. Thanks, I'll try it!

[–] Onomatopoeia@lemmy.cafe 1 points 4 days ago (1 children)

The help file shows it using a variable for the file name.

[–] Flagstaff@programming.dev 1 points 3 days ago

Where? https://www.autohotkey.com/docs/v2/lib/FileAppend.htm

All I see is it using My Script.ahk, not producing My Script DESKTOP_3058.ahk and My Script LAPTOP_362.ahk based on the local machine that the script is run in.

To be transparent, I have a log generator being managed by a script in a Syncthing folder. I'd like to keep the script there if possible, but have the logs be individualized so they don't keep causing sync-conflicts.