Categories
JavaScript Answers

How to get file name from absolute path with Node.js?

Spread the love

Sometimes, we want to get file name from absolute path with Node.js.

In this article, we’ll look at how to get file name from absolute path with Node.js.

How to get file name from absolute path with Node.js?

To get file name from absolute path with Node.js, we can use the path.basename method.

For instance, we write

const path = require("path");
const fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
const file = path.basename(fileName);

to call path.basename with the fileName path string.

As a result, file is 'python.exe'.

Conclusion

To get file name from absolute path with Node.js, we can use the path.basename method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *