459,481 Members | 1,211 Online
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

# This must be easy... help?

 P: n/a Hi there I'm relatively new to javascript, coming from more of a server-side angle. Using javascript, what function/syntax do I use to get ONLY the file-name from the following strings: 1) "C:\ExampleFiles\books\myfavebook.pdf" -> "myfavebook.pdf" 2) "D:\apples.gif" -> "apples.gif" 3) "C:\Program Files\party\bar\drinks\wallbanger.doc" -> "wallbanger.doc" So, I'm looking for the contents of a string after the last "\" character. What's the function/syntax? Much thanks. Sep 13 '05 #1
7 Replies

 P: n/a Good Man wrote: Hi there I'm relatively new to javascript, coming from more of a server-side angle. Using javascript, what function/syntax do I use to get ONLY the file-name from the following strings: 1) "C:\ExampleFiles\books\myfavebook.pdf" -> "myfavebook.pdf" 2) "D:\apples.gif" -> "apples.gif" 3) "C:\Program Files\party\bar\drinks\wallbanger.doc" -> "wallbanger.doc" So, I'm looking for the contents of a string after the last "\" character. What's the function/syntax? Much thanks. When the backslash will always be the path seperator, you could do something as follows: var split, path = "C:\\ExampleFiles\\books\\myfavebook.pdf" var basename = (split = path.split('\\'))[split.length - 1]; alert(basename); Or prototype it for re-use: String.prototype.basename = function () { var split; return (split = this.split('\\'))[split.length - 1]; } alert("C:\\ExampleFiles\\books\\myfavebook.pdf".ba sename()); Mind that the double backslashes are required to indicate a literal backslash, as they will be regarded as escape characters otherwise. JW Sep 13 '05 #2

 P: n/a Janwillem Borleffs wrote in news:43***********************@dr4.euro.net: When the backslash will always be the path seperator, you could do something as follows: var split, path = "C:\\ExampleFiles\\books\\myfavebook.pdf" var basename = (split = path.split('\\'))[split.length - 1]; alert(basename); Or prototype it for re-use: String.prototype.basename = function () { var split; return (split = this.split('\\'))[split.length - 1]; } alert("C:\\ExampleFiles\\books\\myfavebook.pdf".ba sename()); thanks very much... your solution works, though i'm not exactly sure why/how. i will do a little more investigating in regards to this 'prototype' concept. for a free internet muffin and coffee, do you mind explaining your solution a little further? i'll even make it an espresso! Sep 13 '05 #3

 P: n/a Good Man wrote: for a free internet muffin and coffee, do you mind explaining your solution a little further? i'll even make it an espresso! :) Okay, here it goes: The main code is in this line (changed names for clearity): (a = a.split('\\'))[a.length - 1] Where the code between the colons gets parsed first: a = a.split('\\') split is a function of String objects, which turns it into an array. The string gets seperated by the passed argument ('\\' in this case). Now a is an array. Arrays have a length property which contains the number of elements. This property is used to determine the index of the last element, which is length - 1. The result is the last element of the array, which is returned as a string. Coming to think of it, you should probably validate if a.length > 0 before substracting 1, as in: a.length ? a.length - 1 : a.length Which, BTW, is the same as: if (a.length > 0) { a.length - 1; } else { a.length; } Putting it all together results in: (a = a.split('\\'))[a.length ? a.length - 1 : a.length] Now where is my espresso! ;-) HTH; JW Sep 13 '05 #4

 P: n/a Janwillem Borleffs wrote in news:432702ff$0$49769 $d*******@dr6.euro.net: Now where is my espresso! ;-) much thanks Janwillem: http://www.anelixis.gr/images/espresso%201.jpg Sep 13 '05 #5  P: n/a Good Man writes: I'm relatively new to javascript, coming from more of a server-side angle. Using javascript, what function/syntax do I use to get ONLY the file-name from the following strings: 1) "C:\ExampleFiles\books\myfavebook.pdf" -> "myfavebook.pdf" 2) "D:\apples.gif" -> "apples.gif" 3) "C:\Program Files\party\bar\drinks\wallbanger.doc" -> "wallbanger.doc" function filename(pathname) { return pathname.substring(pathname.lastIndexOf("\\")+1); } So, I'm looking for the contents of a string after the last "\" character. Index of last "\" is found using lastIndexOf("\\"). Content after that is found using substring(index+1); /L -- Lasse Reichstein Nielsen - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Sep 13 '05 #6  P: n/a Good Man wrote: I'm relatively new to javascript, coming from more of a server-side angle. Using javascript, what function/syntax do I use to get ONLY the file-name from the following strings: 1) "C:\ExampleFiles\books\myfavebook.pdf" -> "myfavebook.pdf" 2) "D:\apples.gif" -> "apples.gif" 3) "C:\Program Files\party\bar\drinks\wallbanger.doc" -> "wallbanger.doc" How about this , it should work for most things. String("/"+szPath).split("\\").join("/").split("/").pop() -- --. --=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript --=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP --=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL --. Sep 14 '05 #7  P: n/a Good Man wrote: Hi there I'm relatively new to javascript, coming from more of a server-side angle. Using javascript, what function/syntax do I use to get ONLY the file-name from the following strings: 1) "C:\ExampleFiles\books\myfavebook.pdf" -> "myfavebook.pdf" 2) "D:\apples.gif" -> "apples.gif" 3) "C:\Program Files\party\bar\drinks\wallbanger.doc" -> "wallbanger.doc" So, I'm looking for the contents of a string after the last "\" character. What's the function/syntax? Hey, me too!! var filePath = 'C:\ExampleFiles\books\myfavebook.pdf'; alert( filePath.match(/[^\\]+$/) ); // --> myfavebook.pdf It will return everything after the last backslash '\'. -- Rob Sep 14 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.