473,566 Members | 3,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get the filename of a path using String.lastInde xOf()

In test() method:
var path="C:\test\h ello.txt"; //returns -1 for path.lastIndexO f("\\").
why??
var pos=path.lastIn dexOf("\\"); //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\h ello.txt"; //returns -1 for
path.lastIndexO f("\\"). why??
var pos=path.lastIn dexOf("\\"); //unterminate string constant, use
"\\" for backslash
alert("pos=" + pos);
var filename = path.substring( pos+1);
alert(filename) ;
}

function showFile()
{ var path= InputForm.f1.va lue;
alert("path=" + path);
var pos=path.lastIn dexOf("\\"); //unterminate string constant, use
"\\" for backslash
alert("pos=" + pos);
var filename = path.substring( pos+1);
InputForm.filen ame.value = filename;
}
</script>
<form name="InputForm " action="page2.a sp" 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="showF ile()">
</form>
Jul 23 '05 #1
1 16342
On 24 Sep 2004 22:53:53 -0700, Matt <jr********@hot mail.com> wrote:
In test() method:
var path="C:\test\h ello.txt";
//returns -1 for path.lastIndexO f("\\").
why??
The backslash within a literal is an escaping character. The string, path,
above actually produces:

"C:<TAB>esthell o.txt"

which you should see with

alert(path);
var pos=path.lastIn dexOf("\\"); //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
15844
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 image name in the same fashion using =onclick showimage(this). Is this possible at at I have tried all combinations of document.getElementbyId();...
4
22866
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
23368
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 using '/' as delimiters and get the last 'word'. The reason I'm not entirely happy with that method is that it is platform specific. I would prefer...
6
10494
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
25579
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
2279
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
13044
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 generated number from a program that I saved from. So I need to find a way that I can quickly remove the underscore and the last 4 numbers from the...
10
2274
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 following files in order (assuming I am running from the us): help\datasource_en_US.htm help\datasource_en.htm help\datasource.htm
3
1686
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 this page. I am getting the problem that my code is taking the entire path as file name
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.