473,836 Members | 1,519 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("\\").
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;
<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()">
Jul 23 '05 #1
1 16369
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("\\").
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

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,

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

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(); document.getElementbyName(); document.getElementbyTagName();
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?
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 to use a built in method if possible. Cheers,
by: Bruce Vander Werf | last post by:
Is there a method anywhere for extracting just the filename part of a full path name? --Bruce
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.
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
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 filename without changing anything else. I can't set a max length to the file becuase the filename's...
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
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
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.