Skip to content

How to get properly capitalized file path on Windows? #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MartyIX opened this issue Oct 14, 2015 · 4 comments
Closed

How to get properly capitalized file path on Windows? #15

MartyIX opened this issue Oct 14, 2015 · 4 comments

Comments

@MartyIX
Copy link

MartyIX commented Oct 14, 2015

I have a path, let's say C:\temp\something.js and I want to get canonical version of the path on Windows - so if there is C:\Temp\somethinG.js (note the capital letter T and G) stored on disk, I would like to get this value (path). I know that Windows has case-insensitive file system but the file system still knows how user capitalized directory/file names.

How can I get from the former path the later one in Node.js?

I have already gone through FS API (https://nodejs.org/api/fs.html) and I have not found anything useful (namely fs.realpathSync, fs.statSync, fs.accessSync, path.normalize, path.resolve did not return what I need).

Workaround: I have found out that fs.readdir returns correctly capitalized directory/file names but performance hit is too large for any serious usage.

Note: http://stackoverflow.com/questions/4763117/how-can-i-obtain-the-case-sensitive-path-on-windows (similar question to mine)

Thanks!

(Migrated from: nodejs/node#3352)

@Trott
Copy link
Member

Trott commented Oct 14, 2015

path definitely won't do it as that just operates on strings. It doesn't consult the filesystem.

I don't have a Windows machine handy to test on, so take with a grain of salt, but I believe fs.readdir() and fs.readdirSync() might be your best options short of calling out to an external process (say, via child_process) or using (writing) a native module.

@mklement0
Copy link

Find a solution based on the glob package with its nocase option at http://stackoverflow.com/a/33139702/45375

@MartyIX
Copy link
Author

MartyIX commented Oct 15, 2015

Thanks!

@mklement0
Copy link

@MartyIX:

My pleasure; it is surprisingly hard to do this, but it it seems that it is not a feature oversight in Node.js, but lacks system API support in the underlying platforms (both on Windows and OS X).

Btw, you're (also) using the term "canonical" to refer to what you want, which normally means resolving to an absolute path with all symlinks resolved.
The solution on SO I link to does not do that: if you pass in a relative path, you'll get a relative output path as well, and no symlinks are resolved. If you want the canonical path, apply fs.realPathSync() to the result. I've added this information on SO as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants