Page 1 of 1

A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 01:54
by madsounds
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?

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

Posted: 16 Aug 2020, 02:57
by madsounds
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"

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 03:55
by FredOoo
• 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

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 04:20
by madsounds
@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.

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 04:52
by FredOoo
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 ?

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 06:56
by madsounds
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.

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 07:56
by FredOoo
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.

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 08:55
by madsounds
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 :-)

Re: A_LoopFileFullPath doesn't work for a folder

Posted: 16 Aug 2020, 09:39
by FredOoo
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…