Get file from path 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.

answered May 25, 2010 at 18:21

1

Successfully Script for your question ,Full Test





function replaceAll[txt, replace, with_this] {
    return txt.replace[new RegExp[replace, 'g'], with_this];
}

function showSrc[] {
    document.getElementById["myframe"].href = document.getElementById["myfile"].value;
    var theexa = document.getElementById["myframe"].href.replace["file:///", ""];
    var path = document.getElementById["myframe"].href.replace["file:///", ""];
    var correctPath = replaceAll[path, "%20", " "];
   alert[correctPath];
    var filename = correctPath.replace[/^.*[\\\/]/, '']
    $["#FileNameShow"].text[filename]
}

answered May 20, 2016 at 16:54

A simple function like PHP pathInfo:

function pathInfo[s] {
    s=s.match[/[.*?[\\/:]]?[[[^\\/:]*?][\.[^\\/.]+?]?][?:[?#].*]?$/];
    return {path:s[1],file:s[2],name:s[3],ext:s[4]};
}

console.log[ pathInfo['c:\\folder\\file.txt'] ];

console.log[ pathInfo['/folder/another/file.min.js?query=1'] ];
Type and try it:

answered Feb 9 at 18:02

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 Nov 23, 2014 at 19:01

TomasTomas

1,2773 gold badges16 silver badges32 bronze badges

var file_name = file_path.substring[file_path.lastIndexOf['/']];

T J

42.1k13 gold badges81 silver badges135 bronze badges

answered Nov 6, 2013 at 10:38

shriniketshriniket

551 silver badge7 bronze badges

How do I get the file path in HTML?

A file path describes the location of a file in a web site's folder structure. ... File Path Examples..

How do I give a file path in Javascript?

“how to write file paths in javascript” Code Answer.
/ = Root directory..
. = This location..
.. = Up a directory..
./ = Current directory..
../ = Parent of current directory..
../../ = Two directories backwards..

How do I read a file path in node JS?

var fs = require['fs'], path = require['path'], filePath = path. join[__dirname, 'start. html']; fs. readFile[filePath, {encoding: 'utf-8'}, function[err,data]{ if [!

How do I write a file path in node JS?

js: var fs = require['fs'] var newPath = "E:\\Thevan"; var oldPath = "E:\\Thevan\\Docs\\something. mp4"; exports. uploadFile = function [req, res] { fs. readFile[oldPath, function[err, data] { fs.

Chủ Đề