A_LoopFileFullPath doesn't work for a folder Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 01:54

Hi there! I'm using AutoHotkey_2.0-a122-f595abc2. I had to make a function that return file's of folder's full path, including its name. But this function does not always work for folders.

In the script directory, I have files "file", "file.txt" and folder "folder".

Code: Select all

FullPath_v1( filename )
{
	loop files filename
	{
		return A_LoopFileName
	}
}

FullPath_v2( filename )
{
	loop files filename
	{
		return A_LoopFileFullPath
	}
}

MsgBox FullPath_v1( "file" ) ;returns "file"
MsgBox FullPath_v2( "file" ) ;returns "D:\Downloads\file"
MsgBox FullPath_v1( "file.txt" ) ;returns "file.txt"
MsgBox FullPath_v2( "file.txt" ) ;returns "D:\Downloads\file.txt"
MsgBox FullPath_v1( "folder" ) ;returns ""
MsgBox FullPath_v2( "folder" ) ;returns ""
So for a folder that locates in script's directory, A_LoopFileFullPath returns just an empty string instead of path+name string. Is it a bug?
Last edited by BoBo on 16 Aug 2020, 02:13, edited 1 time in total.
Reason: Moved to 'AutoHotkey v2 Help'.
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: A_LoopFileFullPath doesn't work for a folder  Topic is solved

16 Aug 2020, 02:57

Turns out I forgot to add "DF" options :-) This one works well:

Code: Select all

FullPath( filename )
{
	loop files filename, "DF"
	{
		return A_LoopFileFullPath
	}
}

MsgBox FullPath( "folder" ) ;returns "D:\Downloads\folder"
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 03:55

• As soon as break or return is encountered, you exit the loop. So you'll always loop once.
• filename is assumed to be in A_WorkingDir (if an absolute path isn't specified). So you'll always get A_WorkingDir
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 04:20

@FredOoo
> As soon as break or return is encountered, you exit the loop. So you'll always loop once.
It's a trick described somewhere in AHK docs :-) There's no other way to find out file of folder's full path but to get it from A_LoopFileFullPath variable. And this variable is available only inside "loop files" cycle.
> Filename is assumed to be in A_WorkingDir (if an absolute path isn't specified). So you'll always get A_WorkingDir
This function will be used for any path, not only for files in A_WorkingDir.
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 04:52

You're right but, if you ask for the full path of "file.txt", the system must suppose it is somewhere. And it supposes it is in A_WorkingDir.
I think you can gess it without calling any function.
The function you call just checks the file exists in that directory.
If you try this:
FullPath( fileName )
{
return A_WorkingDir "\" fileName
}
I think you will get the same result. Don't you ?
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 06:56

The filename that passes to function might be either short path or full path. This function is needed just to avoid guessing :-) It always return full path.
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 07:56

Saying "to guess" I mean "already know for sure".
And if you pass a fullPath to a function that always returns a fullPath, well… is it necessary ?
It's up to you now.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 08:55

The thing is that paths that are passed to FullPath function, initially stored in INI file. They could be written by another user, so I can't be sure if a path is relative or absolute. Then these paths will be passed to command line utility, where absolute path is preferred to avoid strings like "..\..\..\filename.ext". So this function really makes it easier :-)
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: A_LoopFileFullPath doesn't work for a folder

16 Aug 2020, 09:39

That's right. You can change relatives to absolutes. I will keep this one :

Code: Select all

relativeToAbsoluteFullPath( fileName ) {
	Loop Files fileName, 'DF' {
		return A_LoopFileFullPath
	}
}
MsgBox relativeToAbsoluteFullPath( "..\Downloads\file.txt" ) ;returns "D:\Downloads\file.txt"

By the way, I'm looking for this one :
MsgBox relativeToAbsoluteFullPath( "%SystemRoot%\system32\NOTEPAD.EXE" )
Here…
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 19 guests