Win32 File Name Comparison
An approximate answer is at Comparing Unicode file names the right way.
Basically, the recommendation is to uppercase both strings (using CharUpper
, CharUpperBuff
, or LCMapString
), then compare using a binary comparison (i.e. memcmp or wmemcmp, not CompareString with an invariant locale). The file system doesn't do Unicode normalization, and the case rules are not dependent on locale settings.
There are unfortunate ambiguous cases when dealing with characters whose casing rules have changed across different versions of Unicode, but it's about as good as you can do.
Comparing file names in native code and Don't compare filenames are a couple of good blog posts on this topic. The first has C/C++ code for OrdinalIgnoreCaseCompareStrings, and the second tells you how that doesn't always work for filenames and what to do to mitigate that.
Then there are the Unicode problems. While these new
OrdinalIgnoreCase
string comparison algorithms are great for your local NTFS drive, they might not yield the right answer on your FAT drive, or a network share.So what's the answer? When possible, let the file system tell you.
CreateFile
can tell you if a given filename exists. Just pick the right creation disposition. If you need to compare to handles, you can often useGetFileInformationByHandle
; look atdwVolumeSerialNumber
/nFileIndexHigh
/nFileIndexLow
.