Solved

javascript split on pathname

Posted on 2004-09-16
10
1,864 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
Comment Utility
path = "c:\inetpub\wwwroot\mysite\testfile.txt"
ary = path.split("\\");
filename = ary[ary.length-1];
0
 
LVL 33

Expert Comment

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

Expert Comment

by:knightEknight
Comment Utility
I think the main problem is that you didn't escape the back-slash characters in your original string (see above).
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
or simply:

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

Author Comment

by:LM_vdefra1
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
listing all functions in JavaScript 19 98
javascript email link with image 2 16
Phone Dialer 5 35
Re-position the objects 7 46
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now