nodejs get file name from absolute path?
Solution 1
Use the basename
method of the path
module:
path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'
Here is the documentation the above example is taken from.
Solution 2
To get the file name portion of the file name, the basename method is used:
var path = require("path");
var fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
var file = path.basename(fileName);
console.log(file); // 'python.exe'
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:
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); // 'python'
Solution 3
var path = require("path");
var filepath = "C:\\Python27\\ArcGIS10.2\\python.exe";
var name = path.parse(filepath).name;
// returns
'python'
Above code returns the name of the file without extension, if you need the name with extention use
var path = require("path");
var filepath = "C:\\Python27\\ArcGIS10.2\\python.exe";
var name = path.basename(filepath);
// returns
'python.exe'
Solution 4
For those interested in removing extension from filename, you can use https://nodejs.org/api/path.html#path_path_basename_path_ext
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
Solution 5
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.
Again: Only do this if you're writing for a specific environment, otherwise use the path
module, as paths are surprisingly complex. Windows, for instance, supports /
in many cases but not for e.g. the \\?\?
style prefixes used for shared network folders and the like. On Windows the above method is doomed to fail, sooner or later.
fxp
Updated on July 28, 2022Comments
-
fxp almost 2 years
If there any API could retrieve file name from an absolute file path?
e.g.
"foo.txt"
from"/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, likefile.getName()
in java, could do it.NodeJS get file name from absolute path?
-
Waylon Flinn about 7 yearsIf you also want to remove the extension:
path.basename(fpath, path.extname(fpath))
-
OwnageIsMagic over 6 yearsWindows have \ as folder separator
-
leo over 6 years@OwnageIsMagic Yes, that's why I write “If you already know that the path separator is /”... :)
-
OwnageIsMagic over 6 yearsit's not clear what you meant with this statement. This produces platform dependent code that will silently fail on on other platforms
-
leo over 6 years@OwnageIsMagic Yes, it does indeed. Always use the methods of the path module if you are not writing for a specific platform.
-
RozzA about 6 yearswindows actually accepts both / and \ as folder seperators & you can even mix it up
-
leo about 6 years@RozzA Not always, I'm afraid: msdn.microsoft.com/en-us/library/aa365247.aspx
-
diralik almost 6 yearsAlso this comment is useful
-
JJJ about 5 yearsPlease elaborate your answer.
-
hong4rc over 3 yearsIf you want the file name without the extension, I recommend use:
path.parse(fileName).name