Solved

javascript split on pathname

Posted on 2004-09-16
10
1,878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

621 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