如何在JavaScript中告诉脚本运行的操作系统中使用了哪个路径分隔符?
答案 0 :(得分:98)
在node.js
中使用path
模块返回特定于平台的文件分隔符。
示例
path.sep // on *nix evaluates to a string equal to "/"
编辑:根据Sebas的评论,要使用此功能,您需要在js文件的顶部添加:
const path = require('path')
答案 1 :(得分:19)
即使在Windows上,你总是可以使用/作为路径分隔符。
引自http://bytes.com/forum/thread23123.html:
所以,情况可以总结一下 相当简单:
自DOS 2.0以来所有DOS服务和所有Windows API都接受转发 斜杠或反斜杠。永远有。
标准命令shell(CMD或COMMAND)都不会接受转发 斜杠。甚至是“cd ./tmp”的例子 在上一篇文章中给出的失败。
答案 2 :(得分:4)
VVS的答案是正确的,除了解析由Internet Explorer中的文件输入给出的路径(用IE8测试 - 我不知道其他版本)。在这种情况下,输入元素的值(input.value)给出的路径采用“C:\ fakepath \< filename>”的形式。请注意这里的反斜杠。
答案 3 :(得分:3)
是所有操作系统都接受CD ../或CD .. \或CD ..无论您如何传递分隔符。但是读回一条路呢?你怎么知道它的说法,一个' windows'路径,允许' '
和\
。
例如,当您依赖安装目录%PROGRAM_FILES% (x86)\Notepad++
时会发生什么。以下面的例子为例。
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);
}
}
targetDir
被设置为索引0
和0
之间的子字符串(indexOf('/')
中-1
为C:\Program Files\Notepad\Notepad++.exe
),结果在空字符串中。
这包括以下帖子中的代码:How do I determine the current operating system with Node.js
myGlobals = { isWin: false, isOsX:false, isNix:false };
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; }
操作系统的浏览器端检测
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;
帮助函数获取分隔符
function getPathSeparator(){
if(myGlobals.isWin){
return '\\';
}
else if(myGlobals.isOsx || myGlobals.isNix){
return '/';
}
// default to *nix system.
return '/';
}
// modifying our getDir method from above...
帮助函数获取父目录(跨平台)
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()
必须足够聪明才能知道它在寻找哪个。
如果用户通过命令行输入路径等,您可以获得非常灵活的功能并检查两者。
// 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);
您还可以使用&#39;路径&#39; module和path.sep如上所述,如果你想加载一个模块来完成这个简单的任务。就个人而言,我认为仅仅检查已经可用的流程中的信息就足够了。
var path = require('path');
var fileSep = path.sep; // returns '\\' on windows, '/' on *nix
答案 4 :(得分:1)
正如这里已经回答的那样,您可以使用path.join
找到特定于操作系统的路径分隔符,以手动构建路径。但是您也可以让path.join
来完成这项工作,这是我在处理路径构造时的首选解决方案:
示例:
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
答案 5 :(得分:0)
只要使用“/”,就我所知,它适用于所有操作系统。