Don't compare file names

Yesterday I talked about writing a file name comparison algorithm. There are certainly some code bases (and debuggers are certainly one of them) that need to compare file names. That being said, today I wanted to talk about why you shouldn't compare file names.

First of all, file names can come in many forms. You can include the NTFS stream (example: foo.txt$DATA). You can include a leading '\\?\'. It can be a relative path (example: '..\windows\system32\kernel32.dll'). The same file can be mapped to multiple locations.

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 use GetFileInformationByHandle; look at dwVolumeSerialNumber/nFileIndexHigh/nFileIndexLow.