Skip to content
Advertisement

Access files on linux from nodejs with case insensitivity

I am accessing files from nodejs dynamically, but I am facing a problem when user creates a directory with different case like (/hello) and try to access it with different case like (/Hello). Is there a way that I can access directories on linux through nodejs with case insensitivity?

Advertisement

Answer

On a case-insensitive filesystem your best bet is probably going to be just reading the list of files in the requested file’s directory and do a case-insensitive comparison. For example:

var requestedPath = '...';
fs.readdir(path.dirname(requestedPath), (err, names) => {
  if (err) throw err;

  var requestedFile = path.basename(requestedPath).toLowerCase();

  for (var i = 0; i < names.length; ++i) {
    if (names[i].toLowerCase() === requestedFilename)
      // do something
  }
});
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement