Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


how to get the file name from a full path using javascript


how to get the file name from a full path using javascript  using -'javascript'

Is there a way that I can get the last value (based on the '\' symbol) from a full path?

Example:

C:\Documents and Settings\img\recycled log.jpg


With this case, I just want to get recycled log.jpg from the full path in JavaScript.
    

asked Sep 16, 2015 by nikhilapatil
0 votes
4 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening

answered Sep 16, 2015 by atulpariharmca
0 votes

Just for the sake of performance, I tested all the answers given here:

var substringTest = function (str) {
    return str.substring(str.lastIndexOf('/')+1);
}

var replaceTest = function (str) {
    return str.replace(/^.*(\\|\/|\:)/, '');
}

var execTest = function (str) {
    return /([^\\]+)$/.exec(str)[1];
}

var splitTest = function (str) {
    return str.split('\\').pop().split('/').pop();
}

substringTest took   0.09508600000000023ms
replaceTest   took   0.049203000000000004ms
execTest      took   0.04859899999999939ms
splitTest     took   0.02505500000000005ms

And the winner is the Split and Pop style answer, Thanks to bobince !

answered Sep 16, 2015 by mannumits1
0 votes

The complete answer is:


    
        Testing File Upload Inputs
        
    
    
        
            
            
answered Sep 16, 2015 by ashishshukla
0 votes

A question asking "get file name without extension" refer to here but no solution for that. Here is the solution modified from Bobbie's solution.

var name_without_ext = (file_name.split('\\').pop().split('/').pop().split('.'))[0];
answered Sep 16, 2015 by virendra.bajaj
0 votes

    
        Testing File Upload Inputs
        
    
    
        
            
            
answered Sep 16, 2015 by yogeshplv
0 votes

In Node.js, you can use Path's parse module...



var path = require('path');
var file = '/home/user/dir/file.txt';

var filename = path.parse(file).base;
//=> 'file.txt'
answered Sep 16, 2015 by param.oncemore
0 votes

refer from http://www.codeprojectdownload.com

answered Sep 16, 2015 by atulpariharmca
0 votes
function getFileName(path, isExtension){

  var fullFileName, fileNameWithoutExtension;

  // replace \ to /
  while( path.indexOf("\\") !== -1 ){
    path = path.replace("\\", "/");
  }

  fullFileName = path.split("/").pop();
  return (isExtension) ? fullFileName : fullFileName.slice( 0, fullFileName.lastIndexOf(".") );
}
answered Sep 16, 2015 by okesh.badhiye

...