473,385 Members | 1,324 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,385 software developers and data experts.

How to extract filename with '\'? eg. c:\testfolder\test.txt

109 100+
hi...

i got this filename test.txt with the location of c:\testfolder\test.txt

i can get the full path name from the upload object. But i want to extract only the filename.(test.txt)

i tried using the split command but it gives me error when i run the javascript.
Error: object expected.
May 21 '07 #1
14 2996
dmjpro
2,476 2GB
look at this sample code .....


<input type = file id = test_file_upload>


now the js code ..............

document.getElementById('test_file_upload').value. split('');
//try to use the split function like this what i suggested....
i hope it ll work.

kind regards.
dmjpro.
May 21 '07 #2
skyy
109 100+
look at this sample code .....


<input type = file id = test_file_upload>


now the js code ..............



i hope it ll work.

kind regards.
dmjpro.
Hi thanks for the reply...

i already tried using the split command and it gives me error when i run the script. seems like splitting with '\' doesnt work. i tried '\', "\", '\\', "\\"
May 21 '07 #3
sumittyagi
202 Expert 100+
Hi thanks for the reply...

i already tried using the split command and it gives me error when i run the script. seems like splitting with '\' doesnt work. i tried '\', "\", '\\', "\\"
try using lastIndexOf(), and substring in combination.
fileName = filePath.substring(filePath.lastIndexOf("\\"));
May 21 '07 #4
skyy
109 100+
try using lastIndexOf(), and substring in combination.
fileName = filePath.substring(filePath.lastIndexOf("\\"));
Hi.. this method doesnt work.

i tried the following script:

var path=document.form.file.value;
var fname=path.substring(path.lastIndexOf("\\"));
alert(fname);

It gives me error:Object expected
May 22 '07 #5
sumittyagi
202 Expert 100+
Hi.. this method doesnt work.

i tried the following script:

var path=document.form.file.value;
var fname=path.substring(path.lastIndexOf("\\"));
alert(fname);

It gives me error:Object expected
Your problem lies here:
document.form.file.value

you have named your form as form, but form is treated as keyword in javascript, so change name of your form to frm or abcForm or anything you like (which is not a keyword), then your code will run smoothly.

~~cheers~~
May 22 '07 #6
skyy
109 100+
Your problem lies here:
document.form.file.value

you have named your form as form, but form is treated as keyword in javascript, so change name of your form to frm or abcForm or anything you like (which is not a keyword), then your code will run smoothly.

~~cheers~~
Hi thanks for the reply.

I changed the form name already but it still gives the same error.

document.upload_frm.file.value
May 22 '07 #7
sumittyagi
202 Expert 100+
Hi thanks for the reply.

I changed the form name already but it still gives the same error.

document.upload_frm.file.value
change the name of file element too.
see whatever you give type to input elements those all are treated specially. so you shouldn't use them as names. (for example, text, file, submit, button, radio, etc..)
May 22 '07 #8
skyy
109 100+
change the name of file element too.
see whatever you give type to input elements those all are treated specially. so you shouldn't use them as names. (for example, text, file, submit, button, radio, etc..)
Hi sumittyagi.. its got nothing to do with the form or input names.. i changed it into different name and the same error happens.. thanks..
May 22 '07 #9
sumittyagi
202 Expert 100+
Hi sumittyagi.. its got nothing to do with the form or input names.. i changed it into different name and the same error happens.. thanks..
ok fine, paste you code whatever you are running here. I will see what's wrong with it.
May 22 '07 #10
sumittyagi
202 Expert 100+
ok fine, paste you code whatever you are running here. I will see what's wrong with it.
The code below is running fine.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <script language="javascript" type="text/javascript">
  5. function alertFileName()
  6. {
  7.     var filePath = "C:\\abc\\xyz.txt";
  8.     var fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  9.     alert("filePath is: " + filePath);
  10.     alert("fileName is: " + fileName);
  11. }
  12. </script>
  13. </HEAD>
  14.  
  15. <BODY>
  16. <input type="button" value="Show" onclick="alertFileName()">
  17. </BODY>
  18. </HTML>
  19.  
May 22 '07 #11
skyy
109 100+
The code below is running fine.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <script language="javascript" type="text/javascript">
  5. function alertFileName()
  6. {
  7.     var filePath = "C:\\abc\\xyz.txt";
  8.     var fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  9.     alert("filePath is: " + filePath);
  10.     alert("fileName is: " + fileName);
  11. }
  12. </script>
  13. </HEAD>
  14.  
  15. <BODY>
  16. <input type="button" value="Show" onclick="alertFileName()">
  17. </BODY>
  18. </HTML>
  19.  
Ok thanks... Your code is using the "\\" separator for the file path. It doesnt work when the filepath have "\" as the separator. eg. C:\abc\xyz.txt

Any idea how to do that?
May 22 '07 #12
sumittyagi
202 Expert 100+
ok fine, paste you code whatever you are running here. I will see what's wrong with it.
and if you want to have exactly file element only, then try this code.
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <script language="javascript" type="text/javascript">
  4. function alertFileName()
  5. {
  6.     var filePath = document.forms["testForm"].testFile.value;
  7.     var fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
  8.     alert("filePath is: " + filePath);
  9.     alert("fileName is: " + fileName);
  10.  
  11. }
  12. </script>
  13. </HEAD>
  14.  
  15. <BODY>
  16. <form name="testForm">
  17. <input type="file" name="testFile">
  18. <input type="button" value="Show" onclick="alertFileName()">
  19. </form>
  20. </BODY>
  21. </HTML>
  22.  
May 22 '07 #13
sumittyagi
202 Expert 100+
Ok thanks... Your code is using the "\\" separator for the file path. It doesnt work when the filepath have "\" as the separator. eg. C:\abc\xyz.txt

Any idea how to do that?
dear "\\" is a single character, don't get confused due to that. see the example I gave you for file element.
May 22 '07 #14
skyy
109 100+
Hi sumittyagi..

Thanks alot for your help!

Anyway i manage to find out the error that i made. It is due to my perl script.

Anyway thanks alot!

Cheers!
May 22 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul | last post by:
Hi, Using the code below I can extract the filename from a path but I would like to know how to get just the path too. So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far...
1
by: mickeydisn | last post by:
Sub: C++ Word automation Extract text hello. I want extact text form a word document using a visual c++ programme. I have see a lot of documentation. and my analysis is that I must use a...
6
by: Selen | last post by:
I would like to be able to extract a BLOB from the database (SqlServer) and pass it to a browser without writing it to a file. (The BLOB's are word doc's, MS project doc's, and Excel spreadsheets....
1
by: Joe | last post by:
Hi, I have a filename abcxyz.doc. Can someone tell me how do I extract the file extension .doc from the filename in ASP.NET using VB. For your reference a user is typing a filename in the...
9
by: Scott Reynolds | last post by:
Hello! Could someone please provide me a sample, how to extract filename from url? http://www.mydomain.com/eng/Default.aspx -> Default.aspx Thanks Scott
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
8
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
3
by: SteveB | last post by:
I have posted this question in the Visual Basic 2005 and Visual Basic .Net 2005 discussion groups, also. Hi. I am developing an application/web page with VB.Net that will populate a SQL...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.