如何确定路径OS分离器在JavaScript?
-
02-07-2019 - |
题
我怎么能告诉JavaScript什么路径分离器的使用操作系统中的脚被捉迷藏?
解决方案
Afair你可以始终使用/路径隔离,甚至在窗。
报价 http://bytes.com/forum/thread23123.html:
所以,这种情况可以总结了 而不是简单:
所有DOS服务,因为DOS2.0和所有Windows Api接受要么前进 斜线或反斜杠。总是有。
没有一个标准的命令弹(CMD或命令)将接受前进 斜线。即使是"cd/tmp"的例子 给出一个前后失败。
其他提示
在path
中使用 node.js
模块返回特定于平台的内容文件分隔符。点击
示例
path.sep // on *nix evaluates to a string equal to "/"
编辑:根据Sebas的评论,要使用此功能,您需要在js文件的顶部添加:
const path = require('path')
正确答案
是所有操作系统都接受CD ../或CD .. \或CD ..无论您如何传递分隔符。但是读回一条路呢?你怎么知道它的说法,一个'windows'路径,' '
和\
允许。
明显的'Duh!'问题
例如,当您依赖安装目录%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
和indexOf('/')
(-1
C:\Program Files\Notepad\Notepad++.exe
in getDir()
)之间的子字符串,从而产生空字符串。
解决方案......
这包括以下帖子中的代码:如何使用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);
}
}
<=>必须足够聪明才能知道它在寻找哪个。
如果用户通过命令行输入路径等,您可以获得非常灵活的功能并检查两者。
// 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);
如果你想加载一个模块来执行这个简单的任务,你也可以使用'path'模块和path.sep。就个人而言,我认为仅仅检查已经可用的流程中的信息就足够了。
var path = require('path');
var fileSep = path.sep; // returns '\\' on windows, '/' on *nix
那就是所有人!
VVS的答案是正确的,除了解析由Internet Explorer中的文件输入给出的路径(用IE8测试 - 我不知道其他版本)。在这种情况下,input元素的值(input.value)给出的路径的格式为<!>“C:\ fakepath \ <!> lt; filename <!> gt; <!> quot;。请注意这里的反斜杠。
只要使用<!>“; <!>”,就我所知,它适用于所有操作系统。