7

In my node.js app I have functions which can be passed either

OS-style paths e.g. c:\my\docs\mydoc.doc (or /usr/docs/mydoc.doc or whatever is local)

File URLS e.g. file://c:/my/docs/mydoc.doc (which I'm not sure about the validity of '\'s in??)

Either way, I need to check to see if they refer to a specific location which will always exist as a local OS-style path e.g. c:\mydata\directory\ or /usr/mydata/directory

Obviously for OS-style paths I can just compare them as strings - they SHOULD always be the same (they're created with path) but FILE:// URLS don't necessarily use path.sep and so won't "string match"?

Any suggestions as to the best way to handle this (I'm personally tempted to break EVERYTHING by one-or-more slashes of either sort and then check each piece??

  • Check out path (http://nodejs.org/api/path.html). That should help you. – fakewaffle Aug 20 '13 at 19:13
  • I'm using Path already - problem is it doesn't really understand URLs like file:// and the separator won't necessarily match the OS!? –  Aug 20 '13 at 20:21
  • Using (under Windows) some nodejs code (developed under linux), I wound up at times with both "/" and "\" in the *same* file path! – Fuhrmanator Sep 19 '14 at 01:30

2 Answers2

7

I'm going to post my own take on this - as it came from a suggestion I got from someone on Facebook (no - really!) and it uses path in a way it probably wasn't intended for - e.g. I'm not sure it's the right 'solution' - Im not sure I'm not exploiting path a bit.

The Facebook tip was that path is really just a utility for handing strings with "/" and "\" separators - it ignores everything else - doesn't care what's in there at all.

On that basis, we can use

path.normalize(ourpath)

which will convert all separators to the local OS preferred ones (path.sep)

That means they will match my OS-style directory (which is also made with path) and so I can compare those - without resorting to manually gutting-out slashes...

e.g.

Before

file://awkward/use/of\\slashes\in/this/path

After

file:\awkward\use\of\slashes\in\this\path (Windows)

or

file:/awkward/use/of/slashes/in/this/path (everywhere else)

Removing file:// before (or file: + path.sep after) = local OS-style path!?

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
  • Works for me! And that's exactly why all the built-in node functions don't care about paths like C:\whatever\and/then/unix/slashes.txt – B T Feb 20 '15 at 00:35
0

Just do some manipulation of the string and check to make sure they are the same after correcting for the differences:

var path = require('path');
var pathname = "\\usr\\home\\newbeb01\\Desktop\\testinput.txt";
var pathname2 = "file://usr/home/newbeb01/Desktop/testinput.txt"

if(PreparePathNameForComparing(pathname) == PreparePathNameForComparing(pathname2))
{   console.log("Same path");   }
else
{   console.log("Not the same path");   }

function PreparePathNameForComparing(pathname)
{
    var returnString = pathname;
    //try to find the file uri prefix, if there strip it off
    if(pathname.search("file://") != -1 || pathname.search("FILE://") != -1)
    {   returnString = pathname.substring(6, pathname.length);  }

    //now make all slashes the same
    if(path.sep === '\\')    //replace all '/' with '\\'
    {   returnString = returnString.replace(/\//g, '\\');   }
    else    //replace all '\\' with '/'
    {   returnString = returnString.replace(/\\/g, '/');    }

    return returnString;
}

I checked to see if the URI path name indicator "file://" was there, if so, I deleted it off my compare string. Then I normalized based on the path separator node path module will give me. This way it should work in either Linux or Windows environment.

Brian
  • 3,264
  • 4
  • 30
  • 43
  • That's the sort of thing I had in mind - only problem is that I'm not convinced that file:// URLs won't have '\'s or even a mix of both (I'm only really interested in Chrome here but I know that some browsers support both/mixed)? Hence why I was tempted just to throw-away ALL the separators and compare the remainder!? –  Aug 20 '13 at 20:23
  • If you just want to make them all the same regardless of some crazy mix that might show up, you could just replace them all with , or " " or something like that. You can do that with the following: returnString = returnString(/[\\/]+/, ","); that will replace all '\' and '/' in singles or multiples with ',' so you shouldn't have to worry about them at all anymore. Just use that code instead of the if else in my function and you should get what you are looking for. – Brian Aug 20 '13 at 20:26
  • I accepted this because it contains, in essence, the right answer. My other solution "exploits" path's capabilities - it's the one I'm using but for future visitors the solution is the same - simply remove the protocol (file://) and then change all possible slashes into the local path.sep... –  Aug 22 '13 at 13:53