简体   繁体   中英

How to determine the OS path separator in JavaScript?

如何在 JavaScript 中判断在运行脚本的操作系统中使用了什么路径分隔符?

Use path module in node.js returns the platform-specific file separator.
example

path.sep  // on *nix evaluates to a string equal to "/"

Edit: As per Sebas's comment below, to use this, you need to add this at the top of your js file:

const path = require('path')

Afair you can always use / as a path separator, even on Windows.

Quote from http://bytes.com/forum/thread23123.html :

So, the situation can be summed up rather simply:

  • All DOS services since DOS 2.0 and all Windows APIs accept either forward slash or backslash. Always have.

  • None of the standard command shells (CMD or COMMAND) will accept forward slashes. Even the "cd ./tmp" example given in a previous post fails.

The Correct Answer

all OS's accept CD ../ or CD ..\\ or CD .. regardless of how you pass in separators.的,无论您如何传入分隔符,所有操作系统都接受 CD ../ 或 CD ..\\ 或 CD ..。 But what about reading a path back. How would you know if its say, a 'windows' path, with ' ' and \\ allowed.

The Obvious 'Duh!' Question

What happens when you depend on, for example, the installation directory %PROGRAM_FILES% (x86)\\Notepad++ . Take the following example.

var fs = require('fs');                             // file system module
var targetDir = 'C:\Program Files (x86)\Notepad++'; // target installer dir

// read all files in the directory
fs.readdir(targetDir, function(err, files) {

    if(!err){
        for(var i = 0; i < files.length; ++i){
            var currFile = files[i];

            console.log(currFile); 
            // ex output: 'C:\Program Files (x86)\Notepad++\notepad++.exe'

            // attempt to print the parent directory of currFile
            var fileDir = getDir(currFile);

            console.log(fileDir);  
            // output is empty string, ''...what!?
        }
    }
});

function getDir(filePath){
    if(filePath !== '' && filePath != null){

       // this will fail on Windows, and work on Others
       return filePath.substring(0, filePath.lastIndexOf('/') + 1);
    }
}

What happened!?

targetDir is being set to a substring between the indices 0 , and 0 ( indexOf('/') is -1 in C:\\Program Files\\Notepad\\Notepad++.exe ), resulting in the empty string.

The Solution...

This includes code from the following post: How do I determine the current operating system with Node.js

myGlobals = { isWin: false, isOsX:false, isNix:false };

Server side detection of OS.

// this var could likely a global or available to all parts of your app
if(/^win/.test(process.platform))     { myGlobals.isWin=true; }
else if(process.platform === 'darwin'){ myGlobals.isOsX=true; }
else if(process.platform === 'linux') { myGlobals.isNix=true; }

Browser side detection of OS

var appVer = navigator.appVersion;
if      (appVer.indexOf("Win")!=-1)   myGlobals.isWin = true;
else if (appVer.indexOf("Mac")!=-1)   myGlobals.isOsX = true;
else if (appVer.indexOf("X11")!=-1)   myGlobals.isNix = true;
else if (appVer.indexOf("Linux")!=-1) myGlobals.isNix = true;

Helper Function to get the separator

function getPathSeparator(){
    if(myGlobals.isWin){
        return '\\';
    }
    else if(myGlobals.isOsx  || myGlobals.isNix){
        return '/';
    }

    // default to *nix system.
    return '/';
}

// modifying our getDir method from above...

Helper function to get the parent directory (cross platform)

function getDir(filePath){
    if(filePath !== '' && filePath != null){
       // this will fail on Windows, and work on Others
       return filePath.substring(0, filePath.lastIndexOf(getPathSeparator()) + 1);
    }
}

getDir() must be intelligent enough to know which its looking for.

You can get even really slick and check for both if the user is inputting a path via command line, etc.

// in the body of getDir() ...
var sepIndex = filePath.lastIndexOf('/');
if(sepIndex == -1){
    sepIndex = filePath.lastIndexOf('\\');
}

// include the trailing separator
return filePath.substring(0, sepIndex+1);

You can also use 'path' module and path.sep as stated above, if you want to load a module to do this simple of a task. Personally, i think it sufficient to just check the information from the process that is already available to you.

var path = require('path');
var fileSep = path.sep;    // returns '\\' on windows, '/' on *nix

And Thats All Folks!

VVS's answer is correct, with the exception of parsing a path given by a file input in internet explorer (tested with IE8 - I do not know about other versions). In this case, the path given by the input element's value (input.value) is in the form "C:\\fakepath\\<filename>". Note the backslashes here.

As already answered here, you can find the OS specific path separator with path.join to manually construct your path. But you can also let path.join do the job, which is my preferred solution when dealing with path constructions:

Example:

const path = require('path');

const directory = 'logs';
const file = 'data.json';

const path1 = `${directory}${path.sep}${file}`;
const path2 = path.join(directory, file);

console.log(path1); // Shows "logs\data.json" on Windows
console.log(path2); // Also shows "logs\data.json" on Windows

只需使用“/”,据我所知,它适用于所有操作系统。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM