If there any API could retrieve file name from an absolute file path?
如果有任何API可以从绝对文件路径检索文件名?
e.g. "foo.txt"
from "/var/www/foo.txt"
如。“foo。txt "从" / var / www / foo.txt”
I know it works with string operation, like fullpath.replace(/.+\//, '')
but I want to know is there a more 'formal' way, like file.getName()
in java, could do it.
我知道它可以和字符串操作一起工作,比如fullpath.replace(/)。但是我想知道在java中是否有一个更“正式”的方法,比如file.getName()。
NodeJS get file name from absolute path?
node . js从绝对路径获得文件名?
3 个解决方案
#1
311
Use the basename
method of the path
module:
使用路径模块的basename方法:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
Here is the documentation the above example is taken from.
这是上面示例的文档。
#2
2
If you already know that the path separator is /
(i.e. you are writing for a specific platform/environment), as implied by the example in your question, you could keep it simple and split the string by separator:
如果您已经知道路径分隔符是/(例如,您正在为特定的平台/环境编写),如您问题中的示例所示,您可以保持它的简单性,并通过分隔符分割字符串:
'/foo/bar/baz/asdf/quux.html'.split('/').pop()
That would be faster (and cleaner imo) than replacing by regular expression.
这将比用正则表达式替换更快(在我看来更简洁)。
#3
2
To get the file name portion of the file name, the basename method is used:
要获取文件名的文件名部分,使用basename方法:
var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var file = path.basename(fileName);
console.log(file); If you want the file name without the extension, you can pass the extension variable (containing the extension name) to the basename method telling Node to return only the name without the extension:
console.log(文件);如果您想要没有扩展名的文件名,可以将扩展名变量(包含扩展名)传递给basename方法,让Node只返回没有扩展名的名称:
var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var extension = path.extname(fileName);
var file = path.basename(fileName,extension);
console.log(file);
#1
311
Use the basename
method of the path
module:
使用路径模块的basename方法:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
Here is the documentation the above example is taken from.
这是上面示例的文档。
#2
2
If you already know that the path separator is /
(i.e. you are writing for a specific platform/environment), as implied by the example in your question, you could keep it simple and split the string by separator:
如果您已经知道路径分隔符是/(例如,您正在为特定的平台/环境编写),如您问题中的示例所示,您可以保持它的简单性,并通过分隔符分割字符串:
'/foo/bar/baz/asdf/quux.html'.split('/').pop()
That would be faster (and cleaner imo) than replacing by regular expression.
这将比用正则表达式替换更快(在我看来更简洁)。
#3
2
To get the file name portion of the file name, the basename method is used:
要获取文件名的文件名部分,使用basename方法:
var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var file = path.basename(fileName);
console.log(file); If you want the file name without the extension, you can pass the extension variable (containing the extension name) to the basename method telling Node to return only the name without the extension:
console.log(文件);如果您想要没有扩展名的文件名,可以将扩展名变量(包含扩展名)传递给basename方法,让Node只返回没有扩展名的名称:
var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var extension = path.extname(fileName);
var file = path.basename(fileName,extension);
console.log(file);