473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get the filename of a path using String.lastIndexOf()

In test() method:
var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\").
why??
var pos=path.lastIndexOf("\\"); //return -1

But in showFile() method:
We are able to get the filename. The only difference is test()
hardcode the path,
but in showFile() we gets the path from the file dialog.

Please advise. thanks!!

<script type="text/javascript">
function test()
{ //var path="C:\\test\\hello.txt"; //good, it works
var path="C:\test\hello.txt"; //returns -1 for
path.lastIndexOf("\\"). why??
var pos=path.lastIndexOf("\\"); //unterminate string constant, use
"\\" for backslash
alert("pos=" + pos);
var filename = path.substring(pos+1);
alert(filename);
}

function showFile()
{ var path= InputForm.f1.value;
alert("path=" + path);
var pos=path.lastIndexOf("\\"); //unterminate string constant, use
"\\" for backslash
alert("pos=" + pos);
var filename = path.substring(pos+1);
InputForm.filename.value = filename;
}
</script>
<form name="InputForm" action="page2.asp" method="post">
<P><input type="button" name="b1" value="test file button"
onClick="test()">
<P>File Name: <input type="text" name="filename">
<P><input type="FILE" name="f1" onChange="showFile()">
</form>
Jul 23 '05 #1
1 16318
On 24 Sep 2004 22:53:53 -0700, Matt <jr********@hotmail.com> wrote:
In test() method:
var path="C:\test\hello.txt";
//returns -1 for path.lastIndexOf("\\").
why??
The backslash within a literal is an escaping character. The string, path,
above actually produces:

"C:<TAB>esthello.txt"

which you should see with

alert(path);
var pos=path.lastIndexOf("\\"); //return -1
That's why you need to (and do, correctly) escape the backslash in the
lastIndexOf call, as well.
But in showFile() method:
We are able to get the filename. The only difference is test()
hardcode the path,
but in showFile() we gets the path from the file dialog.


As I already said, with literals, defined with quotes, the backslash acts
as an escaping character. With a value - an evaluated literal, or obtained
from a property - the backslash is just a backslash; it has no meaning.
That's the difference between values and literals.

Hope that makes sense,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sharad Gupta | last post by:
i have this problem of capturing the filename on the instance when onclick is activated in the <body> the function should catch the filename and display it. Second problem, i have to catch the...
4
by: Keith Smith | last post by:
Is there a javascript variable that contains the path of the file from which it ran? Something like this....? alert('My path=' + MyURLPath); Is this possible?
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
6
by: Bruce Vander Werf | last post by:
Is there a method anywhere for extracting just the filename part of a full path name? --Bruce
5
by: Netmonster | last post by:
Hello, How do I parse a string that contains a URL and get the filename only? i.e. string tmp = "http://test.com/downloads/test.exe"; I just want the test.exe.. Thanks in advance.
10
by: John | last post by:
Hi I am using openfiledialog to get user to select files and then I am extracting the files using filenames(). How can I now separate the file path from the actual filename? Thanks Regards
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
10
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the...
3
by: =?Utf-8?B?ZGVlcGFr?= | last post by:
Hi I m facing a strange issue with filename I am attaching an attachment (may be a notepad file, video file or whatever) through a dummy customer page (customer.html) using a text box on...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.