Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript split on pathname

Posted on 2004-09-16
10
Medium Priority
?
1,880 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

916 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