小智 119
使用pathmodule node.js返回特定于平台的文件分隔符.
例
path.sep // on *nix evaluates to a string equal to "/"
Run Code Online (Sandbox Code Playgroud)
编辑:根据Sebas的评论,要使用此功能,您需要在js文件的顶部添加:
const path = require('path')
Run Code Online (Sandbox Code Playgroud)
VVS*_*VVS 18
你可以随时使用/作为路径分隔符,即使在Windows上也是如此.
引自http://bytes.com/forum/thread23123.html:
因此,情况可以简单地概括为:
自DOS 2.0以来所有DOS服务和所有Windows API都接受正斜杠或反斜杠.一直有.
标准命令shell(CMD或COMMAND)都不会接受正斜杠.即使前一篇文章中给出的"cd ./tmp"示例也失败了.
Ben*_*uer 10
正如此处已经回答的那样,您可以找到操作系统特定的路径分隔符来path.sep手动构建路径。但你也可以让它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
Run Code Online (Sandbox Code Playgroud)
是的,无论您如何传递分隔符,所有操作系统都接受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);
}
}
Run Code Online (Sandbox Code Playgroud)
targetDir是被设定为所述指标之间的子串0,和0(indexOf('/')是-1在C:\Program Files\Notepad\Notepad++.exe),导致空字符串。
这包括以下文章中的代码:如何使用Node.js确定当前的操作系统
myGlobals = { isWin: false, isOsX:false, isNix:false };
Run Code Online (Sandbox Code Playgroud)
服务器端检测操作系统。
// 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; }
Run Code Online (Sandbox Code Playgroud)
浏览器侧检测操作系统
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;
Run Code Online (Sandbox Code Playgroud)
辅助函数获取分隔符
function getPathSeparator(){
if(myGlobals.isWin){
return '\\';
}
else if(myGlobals.isOsx || myGlobals.isNix){
return '/';
}
// default to *nix system.
return '/';
}
// modifying our getDir method from above...
Run Code Online (Sandbox Code Playgroud)
Helper函数获取父目录(跨平台)
function getDir(filePath){
if(filePath !== '' && filePath != null){
// this will fail on Windows, and work on Others
return filePath.substring(0, filePath.lastIndexOf(getPathSeparator()) + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
如果要加载模块以完成此简单任务,也可以如上所述使用“ path”模块和path.sep。就个人而言,我认为仅从您已经可用的过程中检查信息就足够了。
var path = require('path');
var fileSep = path.sep; // returns '\\' on windows, '/' on *nix
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39126 次 |
| 最近记录: |