Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update December-13-2021

How to get only the Filename from the Fullpath in javascript (nodejs)

How to get only the Filename from the Fullpath in javascript (nodejs)

Summury Of Article

This article is a reminder of how to get only the filename from the fullpath in javascript (nodejs), with sample code. It is compatible with Windows and Unix systems (including Linux and MacOS).

This is useful when you want to make a conditional branch by file name, or when you want to apply a filter.

My Environment

Software Version
node.js v16.13.0

Get only the filename from path

I'm trying to figure out a way to extract only the trailing filenames from a given FullPath for both Windows and Unix systems. We will consider code for both Windows and Unix systems.

First, let's run the code in nodejs on Windows.

C:\02_data\01_dev\03_nodachisoftweb\contents\sample.js Let's get the file "sample.js" from the path of

 
sample.js
var GetFilenameWindows = (path) => path.split('\\').pop();

var fulllpath_windows = "C:\\02_data\\01_dev\\03_nodachisoftweb\\contents\\sample.js";
console.log(GetFilename(fullpath_windows));

The result of the execution is as follows. The filename was retrieved properly.

result
> node sample.js
sample.js

This is not enough to support Unix-like systems (Linux, MacOS, etc.), so let's define a version of the function that supports both, GetFilenameWindows.

 
sample.js
var GetFilename = (path) => path.split('/').pop().split('\\').pop();

var fullpath_windows = "C:\\02_data\\01_dev\\03_nodachisoftweb\\contents\\sample.js";
var fullpath_unix = "/home/amaji/sample.js";
console.log("from Windows path : " + GetFilename(fullpath_windows));
console.log("from Linux path : " + GetFilename(fullpath_unix));

The result of the execution is as follows.

result
> node sample.js
from Windows path : sample.js
from Linux path : sample.js

There are many other ways to do this, but it's so simple that I'll just post this one for now.

conclusion

Use the following function.

 
get_filename_in_path.js
var GetFilename = (path) => path.split('/').pop().split('\\').pop();

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

How to pass arguments to npm run script

How to pass arguments to npm run script

#Nodejs#Javascript✎ 2021-12-16
Describe how to pass argument to npm run script.
Table Of Contents
How to get only the Filename from the Fullpath in javascript (nodejs)
How to get only the Filename from the Fullpath in javascript (nodejs)
Summury Of Article
Summury Of Article
My Environment
My Environment
Get only the filename from path
Get only the filename from path
conclusion
conclusion
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021