Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.") Topic is solved

Report problems with documented functionality
somethingfly
Posts: 38
Joined: 23 Mar 2015, 17:23

Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

20 Feb 2020, 14:34

So, this drove me crazy, I was looking for a file that started with "con." (it was something con.smith.txt, where Con stood for Connie.) It kept saying FileExist was true, further the FileGetAttribute was A, but FileGetSize was 0. There were no wildcards in it.

Then I find these: https://fossbytes.com/windows-reserved-folder-con-create/ and jeeswg at https://www.autohotkey.com/boards/viewtopic.php?t=26486
It's like renaming a folder to Con, ordinarily it can't be done, but I believe there is a way to force it.
It seems any potential filename that starts with "con." will get a result. I suspect that more of the filenames mentioned in the first link will give false positives (not tested). Furthermore, the documentation for FileExist says:
Since FilePattern may contain wildcards, FileExist may be unsuitable for validating a file path which is to be used with another function or program. For example, FileExist("*.txt") may return attributes even though "*.txt" is not a valid filename. In such cases, FileGetAttrib is preferred.
But even when you do a FileGetAttrib you get "A" for "ARCHIVE", same result as FileExist. And Archive bit is normal for a file. It's not, say, "S" for "SYSTEM". So that's not a real workaround. In fact, I'm thinking that:

a) FileExist("C:\con.") should always give an empty string (or something other than "A", but I'm thinking empty, or at least FileGetAttrib should get an empty string like "*.txt" does) unless there is a "real" file, such as something visible called "con." in "C:"
b) likewise, FileExist("c:\con.txt") should only give a string when there is a "real" file, such a text file you created called "con.txt" in "C:"

Thus, this might be a bug. However, I may be wrong. Regardless, if it isn't a bug, this behavior should be mentioned in the documentation.
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")  Topic is solved

21 Feb 2020, 00:04

When it comes to reserved names, anything goes.

Code: Select all

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
Anything added onto these names with "." and "ext" is not allowed. It has history, dating all the way to PC-DOS.
You may wish to mention it documentation?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
somethingfly
Posts: 38
Joined: 23 Mar 2015, 17:23

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

21 Feb 2020, 12:46

So, do you have an elegant solution for checking for a valid filename? One that I can offer as a suggest for a code example in the documentation (ideally, better than the regex below)?

I guess if FileExist is some inherent function with Windows, and this function is simply calling that system function strictly, without any modification or improvement, then I guess it's a bigger question of whether that should be the case for this function, or all functions, or certain functions, and which functions. Do we simply need another parameter at the end called "valid" that checks if it is a valid filename (i.e. it's a filename you could make by pressing F2 in Windows Explorer--as I am seeing now that I can't even create/rename a file "con.txt")? Do we have a a default AHK function that checks for valid filenames? Does anyone have an elegant user-written function?

Actually, I have in the past resorted to regex to remove offending characters like colons, but that was not enough (hence I came into this problem). A google search finds a better regex by Krzysztof Karski that has these reserved names https://stackoverflow.com/questions/11794144/regular-expression-for-valid-filename :

Code: Select all

\A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|[\s\.])[^\\\/:*"?<>|]{1,254}\z
Is this really the most elegant solution? Or perhaps we can call upon whatever in Windows makes the Rename pop up "The specified device name is invalid" when you try "con.txt" and the tooltip that says "A filename can't contain any of the following characters:" when you try "?.txt"?

FYI, I can post the documentation suggestion here, https://www.autohotkey.com/boards/viewforum.php?f=86 , and that is definitely better than the giant thread that apparently existed before. And while documentation suggestions should be its own subforum and not a giant thread, I think there still needs to be more granularity. In fact,I think that subforum should be split into another subform, with each sub-subforum being each page of the documentation (thus, I could post it on the subsubform for "https://www.autohotkey.com/docs/commands/FileExist.htm" or "commands - FileExist" or somesuch). Or you guys should just breakdown and build a wiki already.
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

27 Feb 2020, 09:15

somethingfly wrote:
21 Feb 2020, 12:46
So, do you have an elegant solution for checking for a valid filename? One that I can offer as a suggest for a code example in the documentation (ideally, better than the regex below)?
Nothing off hand, a thread in Ask for Help would be ideal.
somethingfly wrote:
21 Feb 2020, 12:46
I guess if FileExist is some inherent function with Windows, and this function is simply calling that system function strictly, without any modification or improvement, then I guess it's a bigger question of whether that should be the case for this function, or all functions, or certain functions, and which functions. Do we simply need another parameter at the end called "valid" that checks if it is a valid filename (i.e. it's a filename you could make by pressing F2 in Windows Explorer--as I am seeing now that I can't even create/rename a file "con.txt")? Do we have a a default AHK function that checks for valid filenames? Does anyone have an elegant user-written function?

Actually, I have in the past resorted to regex to remove offending characters like colons, but that was not enough (hence I came into this problem). A google search finds a better regex by Krzysztof Karski that has these reserved names https://stackoverflow.com/questions/11794144/regular-expression-for-valid-filename :

Code: Select all

\A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|[\s\.])[^\\\/:*"?<>|]{1,254}\z
Is this really the most elegant solution? Or perhaps we can call upon whatever in Windows makes the Rename pop up "The specified device name is invalid" when you try "con.txt" and the tooltip that says "A filename can't contain any of the following characters:" when you try "?.txt"?
That expression seems to just about cover the case described in the docs- although the opening \A (a PRCE feature?) doesn't appear to be interpreted by AHK- (`a is, and A alone is fine & the closing \z that omits the final linebreak isn't in the AHK documentation). It looks like it will also miss strings like cOn and LpT4 hence an alternative regex string might go something like (untested):

Code: Select all

A(?!(?:iCOM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX)|[\s\.])[^\\\/:*"?<>|]{1,254}\z
somethingfly wrote:
21 Feb 2020, 12:46
FYI, I can post the documentation suggestion here, https://www.autohotkey.com/boards/viewforum.php?f=86 , and that is definitely better than the giant thread that apparently existed before. And while documentation suggestions should be its own subforum and not a giant thread, I think there still needs to be more granularity. In fact,I think that subforum should be split into another subform, with each sub-subforum being each page of the documentation (thus, I could post it on the subsubform for "https://www.autohotkey.com/docs/commands/FileExist.htm" or "commands - FileExist" or somesuch). Or you guys should just breakdown and build a wiki already.
Thanks, and yes, new ideas for the forum can be either put to Forum Issues or to General Discussion for sure. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
somethingfly
Posts: 38
Joined: 23 Mar 2015, 17:23

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

27 Feb 2020, 18:05

I give a code example here with the regex here: https://www.autohotkey.com/boards/viewtopic.php?f=86&t=72913

The only modification I made to the regex is doubling the quotation mark, which I noticed you did not do. But you are right that yours does catch cOn.txt (once you double the quotation mark). Here it is again, with the modified regex. I ran this on C:\con.txt and C:\cOn.txt and it correctly identified both as invalid:

Code: Select all

InputBox, possiblelocation, Enter a possible location like C:\example.txt or C:\con.txt
SplitPath, possiblelocation, possiblefilename
If !RegExMatch(possiblefilename,"A(?!(?:iCOM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX)|[\s\.])[^\\\/:*""?<>|]{1,254}\z")
    Msgbox % possiblefilename . " is not a valid filename."
else if FileExist(possiblelocation)
    Msgbox % possiblelocation . " exists." 
else
    Msgbox % possiblelocation . " does not exists."
That said, I also stumbled upon a (possible) better check for whether a file exists (that is, returns false when trying does "c:\con.txt" exist): the "test-path" cmdlet in powershell. However, it actually fails as a filename validator (that is, returns true when checking is "c\con.txt" valid by using its own -IsValid operator).

Code: Select all

InputBox, possiblelocation, Enter a possible location like C:\example.txt or C:\con.txt
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("powershell.exe -command test-path -path " . possiblelocation)
output := exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process Close, %pid%
if InStr(output,"True")
    Msgbox % possiblelocation . " exists." 
else
    Msgbox % possiblelocation . " does not exists."
The trade off is this file existence script is slower than the above, but likely more reliable (and I'm not sure about how accurate that regex is). Also, one could combine the two:

Code: Select all

InputBox, possiblelocation, Enter a possible location like C:\example.txt or C:\con.txt
SplitPath, possiblelocation, possiblefilename
If !RegExMatch(possiblefilename,"A(?!(?:iCOM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX)|[\s\.])[^\\\/:*""?<>|]{1,254}\z")
    Msgbox % possiblefilename . " is not a valid filename."
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("powershell.exe -command test-path -path " . possiblelocation)
output := exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process Close, %pid%
if InStr(output,"True")
    Msgbox % possiblelocation . " exists." 
else
    Msgbox % possiblelocation . " does not exists."
Perhaps this code will help others, but as I've mentioned in the other post, I've thrown up my hands on this one.
joefiesta
Posts: 498
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

28 Feb 2020, 11:57

The documentation suggestions has been made into it's own subforum. (I asked that that old, long single thread topic be closed, but that idea was not liked. I have no idea why it should be kept open.)

The new subforum is here: https://www.autohotkey.com/boards/viewforum.php?f=86


make a new entry for each new suggestion. IGNORE the old, long single thread therein.
gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

28 Feb 2020, 12:07

joefiesta wrote:
28 Feb 2020, 11:57
(I asked that that old, long single thread topic be closed, but that idea was not liked. I have no idea why it should be kept open.)
If you mean this topic - it is already locked for some time (and hence, there haven't been any new posts there)
I don't think that additionally deleting or hiding it would have any benefit.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

01 Mar 2020, 02:14

We cant really use the forums to that granularity.
Its simply not made for that.
If we want to dicuss each page it might make way more sense to further develop the documentation software and allowing people to make comments on it.
Recommends AHK Studio
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

02 Mar 2020, 13:18

nnnik wrote:
01 Mar 2020, 02:14
If we want to dicuss each page it might make way more sense to further develop the documentation software and allowing people to make comments on it.
or just make a PR to the docs repo and then people can comment right there about the change, just as github is supposed to be used

joefiesta
Posts: 498
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

02 Mar 2020, 13:23

@gregster - my bad. I thought that long topic was left open. thx. It needed to be closed.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

02 Mar 2020, 14:02

guest3456 I dont think every AHK user has the ability to use github - if we do move to github we will block the majority of users from commenting.
If we do want to comment on every help page an overhaul of the docs might be necessary.
The comments could also be used as an additional resource when the docs cannot be overly specific.
Recommends AHK Studio
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

11 Mar 2020, 17:41

nnnik wrote:
02 Mar 2020, 14:02
The comments could also be used as an additional resource when the docs cannot be overly specific.
i loved how the PHP docs allowed user comments at the bottom of each doc page, would always find helpful nuggets in there

somethingfly
Posts: 38
Joined: 23 Mar 2015, 17:23

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

11 Mar 2020, 23:56

I'm glad others agree that we need page-specific granularity for documentation. I'll say this, if we do comments, I think they should be handled either reddit or amazon style. Reddit style would be that every comment would be rateable, positively or negatively, and could have nested comments, and negative comments hidden under a threshold. Amazon style is you just mark something as helpful or not, but no nesting, and with review numbers near the person's name. Either way, you can click on user's names to see more comments/reviews. I think it should be a mix of both, having positive and negative comments, a threshold, nested comments, and have linked numbers next to name (which might be comments/boardposts, e.g. “26/68“, clicking either pulls up appropriately). Everything can be sorted by new, top, old, etc.

Lastly, here's the best part, unlike reddit's up and down arrows and reply button, to give it a positive you click the “++" symbol, negative is "--", and a reply to a comment is ".=“ . You gotta love that.
pallabibaruah18
Posts: 14
Joined: 20 Jan 2021, 23:20

Re: Bug? FileExist with "con." false positive, e.g. FileExist("C:\con.")

24 Jan 2021, 09:17

Is this really the most elegant solution? Or perhaps we can call upon whatever in Windows makes the Rename pop up "The specified device name is invalid" when you try "con.txt" and the tooltip that says "A filename can't contain any of the following characters:" when you try "?.txt"?

FYI, I can post the documentation suggestion here, https://www.autohotkey.com/boards/viewforum.php?f=86 , and that is definitely better than the giant thread that apparently existed before. And while documentation suggestions should be its own subforum and not a giant thread, I think there still needs to be more granularity. In fact,I think that subforum should be split into another subform, with each sub-subforum being each page of the documentation (thus, I could post it on the subsubform for [External link removed] or "commands - FileExist" or somesuch). Or you guys should just breakdown and build a wiki already.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 5 guests