2

I am using angular and in my .ts file I want to get the filename of a path path without the extension.

So here is the full path + filename:

const fullpath = ["C:\Users\my.user\Desktop\the_filename.txt"]

I've been trying this:

getFileNameOnly() {
  var fileName = "C:\Users\my.user\Desktop\the_filename.txt";
  var res = fileName.substr(fileName.lastIndexOf('.') + 1); 
  console.log(res);
}

This above returns error:

TypeError: fileName.substr is not a function

How can I do this so that the result is:

"the_filename" ?

user13962846
  • 83
  • 2
  • 9
  • What is `data` then? I bet it's not a string, or you've forgotten to the filename to as an argument to the function. – Daniel B Sep 16 '20 at 11:38
  • Sorry, updated question – user13962846 Sep 16 '20 at 11:39
  • Check some answers [here](https://stackoverflow.com/questions/9363145/regex-for-extracting-filename-from-path) or [here](https://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript) – Karmidzhanov Sep 16 '20 at 11:41
  • Well, the code you have most definitely works. Just copy and paste it into your console and you won't get any errors. – Daniel B Sep 16 '20 at 11:41
  • 1
    OK, tried with the console and it's returning .txt (the extension) not the filename – user13962846 Sep 16 '20 at 11:45

3 Answers3

5
var fullpath = "C:/Users/my.user/Desktop/the_filename.txt";
var filename = fullpath.split('/').pop().split('.')[0];

result :- "the_filename"

Edit 1 :

var fullpath = "C:\Users\my.user\Desktop\the_filename.txt";
JSON.stringify(fullpath).split('\\').pop().split('.')[0]

result :- "the_filename"

console image : enter image description here

Nish007
  • 112
  • 6
1

I've corrected your string ("\" has to be "\\" in a string, or it will "escape" u, m, d, ...) and the code:

getFileNameOnly() {
  var fileName = "C:\\Users\\my.user\\Desktop\\the_filename.txt";
  var res = fileName.substr(fileName.lastIndexOf('\\') + 1);
  res = res.substr(0, res.lastIndexOf('.'));  
  console.log(res);
}

And with the table element:

getFileNameOnly() {
  const fullpath = ["C:\\Users\\my.user\\Desktop\\the_filename.txt"];
  var res = fileName[0].substr(fileName.lastIndexOf('\\') + 1);
  res = res.substr(0, res.lastIndexOf('.'));  
  console.log(res);
}

Note that you still need the double-backslash, it's a string.

Cirrus Minor
  • 394
  • 4
  • 14
  • The path is given to me in a variable a does not have double backslash only single backslash – user13962846 Sep 16 '20 at 12:00
  • 1
    If you console.log your variable (try it!), you'll have: "C:Usersmy.userDesktop he_filename.txt ". You should tell the person who provide this data that it is wrong formatted. – Cirrus Minor Sep 16 '20 at 12:05
  • Console shows it like this: ["C:\Users\my.user\Desktop\the_filename.txt"] – user13962846 Sep 16 '20 at 12:10
  • 1
    Look in a minimal Typescript Playground example: https://www.typescriptlang.org/play?#code/G4QwTgBAZglgNgUwHIgLYIgXggIgMIBcAOgKoDOCYZRqAngHQCuFYRAIgmQNYAuA9gAciPABYIA+rEQA7NAno8AHjxwBuAFABjPtLJ9E9OHwDmACinI5ASlVA – Cirrus Minor Sep 16 '20 at 12:15
  • OK sorry about this: I missed the [] ... I'm getting the variable like this: var fileName = ["C:\Users\my.user\Desktop\the_filename.txt"]; – user13962846 Sep 16 '20 at 12:22
  • I've updated my code, but you still need the double-backslash if you hard-code the file path (because, for example, \t is a tab in a string). – Cirrus Minor Sep 16 '20 at 12:50
0

use it this way

 getFileNameOnly() {
  var fileName = "C:\\Users\\my.user\\Desktop\\the_filename.txt";
  var res = fileName.split('\\').pop().split('/').pop(); 
  console.log(res);
}
Aakif
  • 156
  • 6
  • OK sorry about this: I missed the [] ... I'm getting the variable like this: var fileName = ["C:\Users\my.user\Desktop\the_filename.txt"]; – user13962846 Sep 16 '20 at 12:22