FileExist and UNC root path

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

FileExist and UNC root path

25 Aug 2017, 19:04

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)
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: FileExist and UNC root path

26 Aug 2017, 14:03

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
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: FileExist and UNC root path

26 Aug 2017, 15:01

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.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: FileExist and UNC root path

26 Aug 2017, 15:40

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%
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileExist and UNC root path

26 Aug 2017, 15:53

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\*")
Last edited by jeeswg on 26 Aug 2017, 17:53, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: FileExist and UNC root path

26 Aug 2017, 16:43

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.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: FileExist and UNC root path

26 Aug 2017, 17:45

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.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileExist and UNC root path

26 Aug 2017, 17:49

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)\\?$")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: FileExist and UNC root path

26 Aug 2017, 17:53

Oh, my example was not clear enough. I meant UNC with *any* IP adresses or domain with *any* name. Thanks :-)
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileExist and UNC root path

26 Aug 2017, 17:57

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.]+\\?$")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: FileExist and UNC root path

26 Aug 2017, 18:19

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 :-)
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, garry, Google [Bot], Kobaltauge, Rohwedder and 178 guests