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

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

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

Summury Of Article

This is a reminder of how to get the file extension from the full path as a string using javascript (nodejs), with sample code. It is compatible with Windows and Unix systems (including Linux and MacOS).

This is useful for conditional branching by file extension, or when you want to apply a filter.

My Environment

Software Version
node.js v16.13.0

Code

It simply looks like this

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

The file name is retrieved first, and then after the extension "."(dot) are obtained.

An example of a file that does not have "." is not present in the file name, for example, if the file is called "hosts", then the "hosts" t will be retrieved.

For Examples

The following is an example of use.

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

var fullpath_windows = "C:\\02_data\\01_dev\\03_nodachisoftweb\\contents\\sample.png";
var fullpath_unix = "/home/amaji/sample.png";
console.log("Using on Windows :" + GetFileExtension(fullpath_windows));
console.log("Using on Linux :" + GetFileExtension(fullpath_unix));

Here is the result.

result
Using on Windows :png
Using on Linux :png

The extension of the image file, "png", has been retrieved as a string.

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 File Extension from the Fullpath in javascript (nodejs)
How to get only the File Extension from the Fullpath in javascript (nodejs)
Summury Of Article
Summury Of Article
My Environment
My Environment
Code
Code
For Examples
For Examples
Page History
Page History
Nodachisoft © 2021