FileReadLine - using variable as file path

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Kiartan
Posts: 3
Joined: 07 Feb 2023, 05:06

FileReadLine - using variable as file path

Post by Kiartan » 07 Feb 2023, 05:17

Hello!

I am trying to read a specific line of .txt file, but the file path is derived from a variable defined by function. My problem is that FileReadLine gives an empty value if variable is used, but it works properly if I use a direct path. How can I make this work?

My Code:

Code: Select all

path := % PathChecker("Steam: ", "steamapps") ;function giving first part of path to the file
{
	steam_path = %path%\common\game\build-info.txt ;adds second part of path
	FileReadLine, Steam, %steam_path%, 6
; Cuts off needed part of line
	StringTrimLeft, Steam1, Steam, 13
	StringTrimRight, Steam2, Steam1, 2
}
	
MsgBox, %Steam2%
return
If I ask it to return steam_path (eg. MsgBox, %steam_path%) it shows full, proper path. So, the problem occurs within FileReadLine

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: FileReadLine - using variable as file path

Post by ananthuthilakan » 07 Feb 2023, 07:54

Code: Select all

path := % PathChecker("Steam: ", "steamapps") ;function giving first part of path to the file
MsgBox, % path 
return
what do you get ?

User avatar
lmstearn
Posts: 688
Joined: 11 Aug 2016, 02:32
Contact:

Re: FileReadLine - using variable as file path

Post by lmstearn » 07 Feb 2023, 07:58

Hi @Kiartan, and welcome to AHK! :)
FileReadLine, like many other functions must either have a full path including the Drive letter, or the name of a file in the same directory as your script. If you just want to use the filename, then SetWorkingDir to where the file is.
Was able to read the line of a file created in the steamapps folder here:

Code: Select all

MsgBox % PathChecker("C:\Steam\steamapps") ;function giving first part of path to the file
return
PathChecker(path)
{
	steam_path = %path%\common\game\build-info.txt ;adds second part of path
	FileReadLine, Steam, %steam_path%, 6
	msgbox % "Steam " Steam " path " path " steam_path " steam_path " Exists? " FileExist(steam_path)	
; Cuts off needed part of line
	StringTrimLeft, Steam1, Steam, 3
	StringTrimRight, Steam2, Steam1, 2
	return steam2
}
This is not the code you want, sorry, it just illustrates everything is working over here.
Note: The path variable itself is a environment variable, so place #noEnv at the top of the script if you want to use it.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Kiartan
Posts: 3
Joined: 07 Feb 2023, 05:06

Re: FileReadLine - using variable as file path

Post by Kiartan » 07 Feb 2023, 08:14

Thank you and hello!

ananthuthilakan, I get a proper path found by this function (the function itself is in other script, which is #Included in my script)

lmstearn Thanks, I have an idea based on your comment.

Kiartan
Posts: 3
Joined: 07 Feb 2023, 05:06

Re: FileReadLine - using variable as file path

Post by Kiartan » 08 Feb 2023, 04:49

After some experiments I have came to conclusion that AHK does not interprete steam_path as a valid filepath.
If I use

Code: Select all

If FileExist(steam_path)
	MsgBox, True 
Else
	MsgBox, False
It alsways returns False, but using the path in explorer i can easily get to the file i need. How to make the script see this variable content as a valid path?

RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: FileReadLine - using variable as file path

Post by RussF » 08 Feb 2023, 06:44

ananthuthilakan wrote:
07 Feb 2023, 07:54

Code: Select all

path := % PathChecker("Steam: ", "steamapps") ;function giving first part of path to the file
MsgBox, % path 
return
what do you get ?
Kiartan wrote: ananthuthilakan, I get a proper path found by this function (the function itself is in other script, which is #Included in my script)
There was a reason that @ananthuthilakan asked that question, yet you did not answer. No one can help unless you post exactly what is contained in the variable steam_path (or path as was originally requested.) It is entirely possible that the path contains some character(s) that need to be escaped for AHK to work with it.

Russ

Post Reply

Return to “Ask for Help (v1)”