File opening and writing help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BlooDoesntPlay
Posts: 17
Joined: 26 Nov 2021, 07:39

File opening and writing help

Post by BlooDoesntPlay » 07 Dec 2021, 00:53

Hello again AHK community! I got a two part question, if you guys don't mind me asking.
First, I was wondering if it was possible to open a txt file directly into code, like if I were to make a .txt with some lines like

Code: Select all

global A := [1,2]
global B := [10,20]
and if I could open it in the script and access those values directly, not modifying the text file per say but rather creating new variables in the script based off the lines in the .txt file. My other option, which I know would work, would to just assign the numbers from a .txt file into already created variables, but it's going to make my script huge and messy, as it's going to be like 100+ entries.

Two, I can't get this snippet to work. It won't write anything, and if I enter anything in the input box that has a space in it, it freaks out and says it's an illegal character, so how am I supposed to write spaces to a txt file?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

global TestFile := FileOpen("testingfile.txt","rw")
global NumbersToAdd := [100,200]
global ThingsToAdd := 4

^+0::
	TestFile.Pos := 0
	index := 0
	while (index < ThingsToAdd)
	{
		index += 1
		InputBox, testinput, Test, Put the number here, , 300,300
		ToolTip %testinput%
		TestFile.WriteLine(%testinput%)
	}
	FileRead, Readed, "testingfile.txt"
	
	MsgBox %Readed%
return
Also, after closing out the script so I can view the text file, the file is all super jank. I'll fill it with filler text just so I can see how it's being edited, and the end result doesn't ever look like what I'm expecting.

Thanks in advance for the help!

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: File opening and writing help

Post by boiler » 07 Dec 2021, 01:49

BlooDoesntPlay wrote: First, I was wondering if it was possible to open a txt file directly into code, like if I were to make a .txt with some lines like

Code: Select all

global A := [1,2]
global B := [10,20]
and if I could open it in the script and access those values directly, not modifying the text file per say but rather creating new variables in the script based off the lines in the .txt file.
You can use #Include to make the lines in a file execute as part of the script.

BlooDoesntPlay wrote: Two, I can't get this snippet to work. It won't write anything, and if I enter anything in the input box that has a space in it, it freaks out and says it's an illegal character, so how am I supposed to write spaces to a txt file?
Don’t use % around a variable in expression syntax, which is what is used in function parameters. By doing that, you are saying you want to use the variable named whatever the contents of the variable testinput are, and spaces are not allowed in variable names, hence the error. So this line:

Code: Select all

		TestFile.WriteLine(%testinput%)
…should be:

Code: Select all

		TestFile.WriteLine(testinput)

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: File opening and writing help  Topic is solved

Post by Xtra » 07 Dec 2021, 01:55

To add to the above post:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

global TestFile := FileOpen("testingfile.txt","rw")
global NumbersToAdd := [100,200]
global ThingsToAdd := 4

^+0::
	TestFile.Pos := 0
	index := 0
	while (index < ThingsToAdd)
	{
		index += 1
		InputBox, testinput, Test, Put the number here, , 300,300
		ToolTip %testinput%
		TestFile.WriteLine(testinput)    ; do not use % in function parameters as they use expression syntax
	}
    TestFile.Close()                     ; close the file
    ToolTip
    
	FileRead, Readed, testingfile.txt    ; not expression syntax so do not quote the filename
	
	MsgBox %Readed%
    
    Readed := RTrim(Readed, "`r`n")
    A := StrSplit(Readed, "`n", "`r")    ; create array from file contents
    
    for key, val in A
        MsgBox, 4096, array test values, % "key= " . key . "`nval= " . val
return

BlooDoesntPlay
Posts: 17
Joined: 26 Nov 2021, 07:39

Re: File opening and writing help

Post by BlooDoesntPlay » 07 Dec 2021, 02:23

Thanks guys, great answers. I'm also super appreciative of the file reading bit, I was going to end up doing that myself but Xtra's solution was going to be far cleaner than mine; I was going to end up basically recreating RTrim as I didn't realize there was a function for it. You guys are the best!

Post Reply

Return to “Ask for Help (v1)”