Page 1 of 1

FileExist and UNC root path

Posted: 25 Aug 2017, 19:04
by JnLlnd
Hi,

Is this normal that the FileExist function returns an empty string (= false) when checking for the existence of the root of an UNC path? Any reason for this?

For example:

Code: Select all

#SingleInstance force
MsgBox, % FileExist("\\127.0.0.1") ; returns empty (= false)
MsgBox, % FileExist("\\127.0.0.1\") ; returns empty (= false)
MsgBox, % FileExist("\\127.0.0.1\Users\") ; returns "RD" (= true)
MsgBox, % FileExist("C:\") ; returns "SHD" (= true)

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 14:03
by JnLlnd
An AutoIt user reports these results with FileExists command:

AutoIt Code:

Code: Select all

MsgBox(1,"test", FileExists("\\127.0.0.1")) ; returns false
MsgBox(1,"test", FileExists("\\127.0.0.1\")) ; returns false
MsgBox(1,"test", FileExists("\\127.0.0.1\mp3")) ; returns true
MsgBox(1,"test", FileExists("C:\")) ; returns true

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 15:01
by obeeb
If AutoIt does the same thing and PathFileExists which is probably what Autohotkey and AutoIt use describes it as the correct behavior then I would say this is normal.

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 15:40
by JnLlnd
Thanks for posting this page. The answer is here:
A path specified by Universal Naming Convention (UNC) is limited to a file only; that is, \\server\share\file is permitted. A UNC path to a server or server share is not permitted; that is, \\server or \\server\share. This function returns FALSE if a mounted remote drive is out of service.
It is normal but, in my view, just not logic :-)

Since an UNC root is not permitted, I checked if A_LastError would flag it. But no: A_LastError = 0.

Code: Select all

#SingleInstance force
bln := FileExist("\\127.0.0.1") ; returns empty (= false)
MsgBox, %A_LastError%

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 15:53
by jeeswg
That's interesting. I knocked up some quick RegEx:

Code: Select all

q::
vList := " ;continuation section
(
\\127.0.0.1
\\127.0.0.1\
\\127.0.0.1\Users\
C:\
)"
Loop, Parse, vList, `n, `r
{
	vPath := A_LoopField
	MsgBox, % RegExMatch(vPath, "^\\\\\d{3}\.\d{1}\.\d{1}\.\d{1}\\?$") "`r`n" vPath
}
return
Can you use this?

Code: Select all

MsgBox, % FileExist("\\127.0.0.1\*")

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 16:43
by obeeb
JnLlnd wrote:It is normal but, in my view, just not logic :-)
Cant argue with that ;). I guess the people who made it this way thought it won't be logical to treat a network share as a file.

After checking Autohotkey source code it actually uses GetFileAttributes. And FindFirstFile when you use a wildcard.
They both suffer from the same problem so @jeeswg's suggestion unfortunately won't help. You can try to use WNetGetResourceInformation with DllCall I think it will do what you want.

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 17:45
by JnLlnd
jeeswg wrote:That's interesting. I knocked up some quick RegEx:
...
Interesting, can I use your RegEx brain for a minute (or two)? What would be the RegEx expresion used with RegExMatch that would return true for the following paths and false for any other path?

\\127.0.0.1
\\127.0.0.1\
\\MyDomain
\\MyDomain\

This would help me make a workaround. Not checking if file (folder) exists when it is an UNC root would be sufficient for my need.
jeeswg wrote:Can you use this?

Code: Select all

MsgBox, % FileExist("\\127.0.0.1\*")
Unfortunately, it returns the same result. Same for "\\127.0.0.1\*." or "\\127.0.0.1\*.*".
obeeb wrote:Cant argue with that ;). I guess the people who made it this way thought it won't be logical to treat a network share as a file.
Not a file I agree, but it could be treated as a folder...
obeeb wrote:After checking Autohotkey source code it actually uses GetFileAttributes.
GetFileAttributes wrote:If you call GetFileAttributes for a network share, the function fails, and GetLastError returns ERROR_BAD_NETPATH. You must specify a path to a subfolder on that share.
Wouldn't it be possible to retrieve this error in AHK after FileExist? A_ErrorLevel is useless actually.

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 17:49
by jeeswg
My RegEx example above can handle:
\\???.?.?.?
\\???.?.?.?\

RegEx for these 4 would be:
\\127.0.0.1
\\127.0.0.1\
\\MyDomain
\\MyDomain\

Code: Select all

MsgBox, % RegExMatch(vPath, "^(\Q\\127.0.0.1\E|\Q\\MyDomain\E)\\?$")

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 17:53
by JnLlnd
Oh, my example was not clear enough. I meant UNC with *any* IP adresses or domain with *any* name. Thanks :-)

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 17:57
by jeeswg
You can replace \d{1} with say \d{1,2}. To specify a certain number of digits.
This currently handles: '\\???.?.?.?' and '\\???.?.?.?\'.

Code: Select all

RegExMatch(vPath, "^\\\\\d{3}\.\d{1}\.\d{1}\.\d{1}\\?$")
I'm not really familiar enough with the possibilities to be more specific.
Perhaps this, \\ then letters/digits/. and an optional trailing \.

Code: Select all

RegExMatch(vPath, "^\\\\[A-Za-z0-9.]+\\?$")

Re: FileExist and UNC root path

Posted: 26 Aug 2017, 18:19
by JnLlnd
jeeswg wrote:Perhaps this, \\ then letters/digits/. and an optional trailing \.
Thank you jeeswg. I'll work on it later and stop hijacking this thread for RegEx help :-)