Solved

javascript split on pathname

Posted on 2004-09-16
10
1,870 Views
Last Modified: 2008-01-09
i have a pathname:

path = "c:\inetpub\wwwroot\mysite\testfile.txt"

how would i seperate this pathname into a variable containing only the filename?
i have been trying unsuccessfully for about an hour to accomplish this using the split() method.
here is my code...

var filename = path.split('\\');
alert (filename[0]);

the result is a string stored in only the first element of the array, and that string is:
"c:inetpubwwwrootmysite testfile.txt"

anything element after filename[0] is undefined.
0
Comment
Question by:LM_vdefra1
  • 6
  • 3
10 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 12078917
path = "c:\inetpub\wwwroot\mysite\testfile.txt"
ary = path.split("\\");
filename = ary[ary.length-1];
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12078920
oops ...
path = "c:\\inetpub\\wwwroot\\mysite\\testfile.txt"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12078925
I think the main problem is that you didn't escape the back-slash characters in your original string (see above).
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 18

Expert Comment

by:bobbit31
ID: 12079018
or simply:

path = "c:\inetpub\wwwroot\mysite\testfile.txt"
filename = path.substring(path.lastIndexOf("\\"));
0
 

Author Comment

by:LM_vdefra1
ID: 12079139
You're correct Knight. So would you suggest inserting those extra '\' into the pathname in order to extract the filename, or is there a simpler solution?
(bobbit, the substring doesnt seem to work because of what knight stated above. The backslash characters in the original pathname are ignored.)
IE. when i just throw up the path name as:
alert ("c:\inetpub\wwwroot\mysite\testfile.txt");
it displays...
"c:inetpubwwwrootmysite testfile.txt"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12079148
yes ...
bobbit's will also work if you escape the back-slashes:

path = "c:\\inetpub\\wwwroot\\mysite\\testfile.txt";
filename = path.substring(path.lastIndexOf("\\"));
0
 

Author Comment

by:LM_vdefra1
ID: 12079220
ok. the path i'm using is specified by a user through a <input type = file> element in a form, so i cant just hardcode the back-slashes back in with '\\'.
is there a 'replace' function or something similar which would allow me to specifiy the character to replace and what to replace it with?

(i'm still new to javascipt in case you couldn't tell. i apologize for asking questions that might seem to have obvious answers)
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 12079275
this works for me as is:

<HTML>
<HEAD>
<SCRIPT language="javascript">
 function doit()
 {
   var path = document.myform.myfile.value;
   var filename = path.split("\\");
   alert(filename[filename.length-1]);
 }
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform">
<INPUT type="file" name="myfile" onchange="doit();" />
</FORM>
</BODY>
</HTML>
0
 

Author Comment

by:LM_vdefra1
ID: 12079380
This is interesting...
when i specify the pathname as you did knight... just referencing the inputs value property, the split works fine.
var path = document.myform.myfile.value;

 my problem occured when i passed the pathname in as a parameter to a javascript function.
EX.
function doit(path)
 {
   var path = document.myform.myfile.value;
   var filename = path.split("\\");
   alert(filename[filename.length-1]);
 }

i can work through this, now that i know what the problem was.
Thanks knightEknight.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12079412
this should also work:

function doit(path)
 {
   var filename = path.split("\\");
   alert(filename[filename.length-1]);
 }

<INPUT type="file" name="myfile" onchange="doit(this.value);" />
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question