• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1883
  • Last Modified:

javascript split on pathname

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
LM_vdefra1
Asked:
LM_vdefra1
  • 6
  • 3
1 Solution
 
knightEknightCommented:
path = "c:\inetpub\wwwroot\mysite\testfile.txt"
ary = path.split("\\");
filename = ary[ary.length-1];
0
 
knightEknightCommented:
oops ...
path = "c:\\inetpub\\wwwroot\\mysite\\testfile.txt"
0
 
knightEknightCommented:
I think the main problem is that you didn't escape the back-slash characters in your original string (see above).
0
Independent Software Vendors: 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!

 
bobbit31Commented:
or simply:

path = "c:\inetpub\wwwroot\mysite\testfile.txt"
filename = path.substring(path.lastIndexOf("\\"));
0
 
LM_vdefra1Author Commented:
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
 
knightEknightCommented:
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
 
LM_vdefra1Author Commented:
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
 
knightEknightCommented:
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
 
LM_vdefra1Author Commented:
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
 
knightEknightCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now