Getting actual file name (with proper casing) on Windows


Windows file system is case insensitive. How, given a file/folder name (e.g. "somefile"), I get the actual name of that file/folder (e.g. it should return "SomeFile" if Explorer displays it so)?

Some ways I know, all of which seem quite backwards:

  1. Given the full path, search for each folder on the path (via FindFirstFile). This gives proper cased results of each folder. At the last step, search for the file itself.
  2. Get filename from handle (as in MSDN example). This requires opening a file, creating file mapping, getting it's name, parsing device names etc. Pretty convoluted. And it does not work for folders or zero-size files.

Am I missing some obvious WinAPI call? The simplest ones, like GetActualPathName() or GetFullPathName() return the name using casing that was passed in (e.g. returns "program files" if that was passed in, even if it should be "Program Files").

I'm looking for a native solution (not .NET one).



FindFirstFileNameW will work with a few drawbacks:

  • it doesn't work on UNC paths
  • it strips the drive letter so you need to add it back
  • if there are more than one hard link to your file you need to identify the right one
By : Doub

There is another solution. First call GetShortPathName() and then GetLongPathName(). Guess what character case will be used then? ;-)

By : sergioko

This video can help you solving your question :)
By: admin