Unfortunately, directory/file names in Windows are case-insensitive.
When I compare a text (input from user) with a directory name (coming from CFileFind
), how can I check whether they mean the same directory or not?
For example C:\PIPPO\
and C:\Pippo\
are the same directory, while C:\Pippò\
is not the same (the last one has an accent).
I'm trying with:
if(CompareString(LOCALE_INVARIANT,NORM_IGNORECASE,q,-1,data_from_CfileFind->txt.GetBuffer(),-1)==CSTR_EQUAL)
(q
is [part of] the user input)
It "kind of works", as it recognizes as same directory the case variations of Roman, Greek and Cyrillic alphabets, but it confuses "weiß"
and "weiss"
(and they are two different directories on my disc), so it's not reliable.
[the failing test is inspired by Comparing and sorting Unicode filenames : I have read it, but found no suitable solution - the links appear not to work)
(I also read Windows Invariant Culture Puzzle but I'm afraid I didn't fully understand about the "cultures").
Any suggestion?
Maybe I should call CompareString()
with different parameters? Or is there a better, easier approach?
Please note that I don't need to sort names: I'd just like to check whether they mean the same directory (or file) to Windows or not.
By "Windows", I mean from 2000 (or at least XP) and later.
EDIT (sorry, the question was not well-asked the first time)
1) The path input by the user is not guaranteed to refer to an actually existing directory (in this case, of course, they are not the same directory).
2) I know that files and directories can be referred to by very different names, because of links (hard or soft), subst
s, network access with different name or IP to the same computer, etc... but I'm not asking to detect all of those cases.
All I'd like to check is whether a name written by the user is a case-variation of another existing one (and thus, for example, Windows would tell me that the file already exists if I try to create one with the same name but different case).
2nd EDIT
This does the job (at least, in the cases that I tried):
if(CompareStringOrdinal(q,-1,data_from_CfileFind->txt.GetBuffer(),-1,1)==CSTR_EQUAL)
But CompareStringOrdinal()
is not available in older Windows versions.
Is there any equivalent?