Solved

Remove file path

Posted on 2000-04-19
30
227 Views
Last Modified: 2010-04-09
I need to remove the path of a file name.  
My page will browse and retrieve a file.  ex. c:\documents\myfile.doc
I want to be able to use just myfile.doc elsewhere on the page without the path.

Thank you in advance for any help you can provide.

Mike

0
Comment
Question by:mcarollo1
  • 15
  • 15
30 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731403
You can do this with javascript:

<SCRIPT language='javascript'>

var path = "c:\documents\myfile.doc";
var ary = path.split("\");
var filename = ary[ary.length-1];
alert(filename);

</script>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731432
Lets make a reusable function out of it ...

<SCRIPT language='javascript'>

 function extractfilename(path)
 {
    path = path.split("/").join("\");  // convert "/" to "\"
    var ary = path.split("\");
    return( ary[ary.length-1] );
 }

 var mypath = "c:\temp\myfile.doc";
 var myfile = extractfilename(mypath);
 alert(myfile);

</script>
0
 

Author Comment

by:mcarollo1
ID: 2731491
Then how can I incorporate the Javascript into the HTML.
i.e.
a file path (c:\documentmyfile.doc) is entered into textbox 1 which runs a pearl script.

The information from textbox 2 is entered into a database.  I want to strip the path and automatically enter just myfile.doc into textbox 2.



0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 33

Expert Comment

by:knightEknight
ID: 2731542
<HTML>
<HEAD>
<SCRIPT language='javascript'>

 function extractfilename(path)
 {
    path = path.split("/").join("\");  // convert "/" to "\"
    var ary = path.split("\");
    return( ary[ary.length-1] );
 }

</script>
<head>

<BODY>
<FORM name='myform' action='whatever.asp' method='post'
  onSubmit='this.textbox2.value=extractfilename(this.textbox1.value);return(true);'>
<INPUT type='text' name='textbox1' value='C:\temp\myfile.doc'><BR>
<INPUT type='text' name='textbox2'><BR>
<INPUT type='submit'>
</form>
</body>
</html>
0
 

Author Comment

by:mcarollo1
ID: 2731594
That will not update the second text box.  It will leave the second box bland, and use the value in the background.  I want either to fill in the second box automatically with the correct value, or not display the second box, which this also does not allow.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731628
Sorry, I had a couple of typos in the function  Try this:

 function extractfilename(path)
 {
    path = path.split("/").join("\\");
    var ary = path.split("\\");
    return( ary[ary.length-1] );
 }
0
 

Author Comment

by:mcarollo1
ID: 2731713
That works great.  However, how you currently have it set up, the second box fills in after you hit the submit button.  Is there a way to have it dynamically update when you change the value of the first box?

Additionally, but off topic from this question, do you know of a way to remove spaces from that file name? ex. I want my file 1.doc to become myfile1.doc.
I have my perl script already performing that function, but to have the two names match up, I also need the box 2 to display that.

The answer to the second question is not necessary, but if you can help I would appreciate it.

thanks,
Mike
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731762
I think I can do both.  Change the first textbox like this:

<INPUT type='text' name='textbox1' value='C:\temp\myfile.doc'
  onChange='this.value=this.value.split(" ").join("");this.form.textbox2.value=extractfilename(this.value);'><BR>

This won't change it on the fly, but it will change it as soon as the user tabs (or clicks) out of the first textbox -- after thay have made their changes.

To do it dynamically, I tried onKeyUp instead of onChange, and it worked -- but with each keystroke the cursor got placed at the end of the text, even if I was typing in the middle, so I didn't think you would want that.

Also, spaces are now removed (from both).  However, there is nothing preventing the user from changing the second textbox after it has been given a value.  But you can include this in the second text box to deal with that:

onFocus='this.blur();'
0
 

Author Comment

by:mcarollo1
ID: 2731826
Great!
Just one question, the line  onFocus='this.blur();' doesn't produce a function, it is printing onFocus='this.blur();' on the screen under the text boxes.  Any ideas?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731855
What I meant was to put it in the textbox tag like this:

<INPUT type='text' name='textbox2' onFocus='this.blur();' >

That should do it  :)
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 2731867
Also, is it even necessary for the user to see the second text box, since it is really only for the server?  If not, just make it a hidden field:

<INPUT type='hidden' name='textbox2' value=''>

It's value will still be changed by the code above, and the server will still see only the filename in "textbox2" -- but the user won't see it.
0
 

Author Comment

by:mcarollo1
ID: 2731962
Thanks!  That worked perfectly.

I don't want to hold up your points any longer, so I am closing this out now, but I do have another question if you don't mind...

When I press the submit button, I want box 1 to go out to a perl script and box 2, along with other enteries to go into a database.  I have the perl script and database connection in place and I am currently using 2 frames with 2 seperate submit buttons.  Can I combine into 1 frame and 1 submit button, and have the two functions occur seperately?

Thanks again for your help

Mike
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2731986
>> I am currently using 2 frames with 2 seperate submit buttons.

How are you getting the values in the second frame into the database?  Are you using a PERL script for that as well?  If so, is it the same PERL script that the first frame executes?
0
 

Author Comment

by:mcarollo1
ID: 2732005
No, I am connecting to an access database using an asp
The pearl script uploads a file to a storage location and the database holds text info about it
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2732137
OK, so in one frame you have a form that executes a PERL script, and in the other frame you have a form that executes an ASP page:

<FORM name='form1' method='post' action='myperl.pl'>
...
<FORM name='form2' method='post' action='myasp.asp'>

Is this correct?
0
 

Author Comment

by:mcarollo1
ID: 2732148
correct, and I want them to be in the same frame and share a submit button
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2732177
I assume then that the ASP does not return an HTML page -- but serves only to insert data into the database.  Is this correct?
0
 

Author Comment

by:mcarollo1
ID: 2732232
yes, that's correct
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2732335
There are two ways.  The single-form dual-post way is hard (and is mis-named, since you can't really do that).  First let's combine the two submits:

Change your existing form1 tag to do this:

<FORM name='form1' method='post' action='myperl.pl' onSubmit='top.NameOfTheOtherFrame.form2.submit();return(true);'>

Then you can get rid of the submit button in the second frame.


The one-frame way is complex....
I suggest that you submit the form to the ASP page, which then inserts the proper values into the DB, and then returns a page to the browser that contains a hidden form that immediately (and inivisibly) submits itself to the perl script.  This hidden form is populated with the same fields and data as the visible form on the previous page.  So the output of the ASP page would look something like this:

<HTML>
<BODY onLoad='document.form2.submit();'>
<FORM name='form2' method='post' action='myperl.pl'>
 <INPUT type='hidden' name='Field1' value='<%=Request("Field1")%>'>
 <INPUT type='hidden' name='Field2' value='<%=Request("Field2")%>'>
 <INPUT type='hidden' name='Field3' value='<%=Request("Field3")%>'>
</form>
</body>
</html>

As soon as this page loads, it submits to the perl script, which then produces the resulting (visible) HTML page.
0
 

Author Comment

by:mcarollo1
ID: 2734582
In the second entry - for the one frame, I don't completly understand what you are doing.  Also, i don't know if this will make it any easier, but the same textboxes are not going to the same place.  box 1 only goes to perl, and the rest go to the database, excluding box 1.
0
 

Author Comment

by:mcarollo1
ID: 2734889
If I keep it with 2 frames, then the work we did yesterday will not apply.

In your single frame suggestion, are you repopulating the html form with information from the database?  If so, then the information I want to go into perl will not be available as it does not go into the database.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2735075
In the single frame suggestion, what I mean is this -- send _all_ the data (text boxes) to the ASP page -- but only put the values you want in the database.  Then, if you only want textbox1 to go to the perl script, you only need to include it in the hidden form:

<FORM name='form2' method='post' action='myperl.pl'>
 <INPUT type='hidden' name='textbox1' value='<%=Request("textbox1")%>'>
</form>
0
 

Author Comment

by:mcarollo1
ID: 2735118
Where is requesting the textbox1 info from?  The database or the previous browser page?

Also, I need to modify your solution to my question about removing the spaces from the file name entry.  The way you have it setup, the spaces are removed from textbox1 and that is moved to textbox 2.  I need the infor in box 1 to remain intact (leave spaces in) and the filename in box 2 to have no spaces, can that be done?

Mike

PS If you would like, I will post this as another question so you can get more points.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2735512
The ASP Request method always gets the value from the previous page (form) -- not the database.  You may have seen it in 2 other variations:  Request.Form and Request.QueryString.  If Request by itself doesn't work, then try Request.Form("textbox1").

As to the spaces question, do this:

<INPUT type='text' name='textbox1' value='C:\temp\myfile.doc'
  onChange='this.form.textbox2.value=extractfilename(this.value).split(" ").join("");'><BR>
0
 

Author Comment

by:mcarollo1
ID: 2736309
The request gommand is not working...
It is seeing the request command as text and filling the box with that
0
 

Author Comment

by:mcarollo1
ID: 2737655
I changed the imput type to text so I can see what it is putting in the new box.  The value in the box is '<%=Request("textbox1")%>'>
It aparently thinks that it is text, not a function
 
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2738229
The symbols <% and %> are recognized by the ASP server -- so you can't just name the file something.asp and expect it to work -- you must run this on an ASP enabled web server ( presumably IIS )
0
 

Author Comment

by:mcarollo1
ID: 2738255
It is an IIS server.  And the first page is already an asp connecting sucessfully to the database.
0
 

Author Comment

by:mcarollo1
ID: 2755499
Quick question:
I have two fields where info is entered into the first box, then manipulated and inserted into the second box.  I have used onchange, and onkeyup  and they both work great for me.  However, if i change the box type to "file" the second box is not populated unless I put the cursor in the first and tab out of it.  Users will not do this when it is a file box with a browse button.  Is there another operator that will suite my purposes better?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2755619
the <INPUT type='file'  element has several special properties that will not let you do certain things, setting the value via javascript is one thing you cannot do to this type of control.  This is for security reasons to prevent low-lifes from doing this:

<BODY onLoad='document.myform.myfile.value="c:\\windows\\passwords.txt";document.myform.submit();'>
<FORM name='myform' action='http://myserver.com/myaction.cgi'>
<INPUT type='file' name='myfile'>
</form>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compact pure CSS Read More Toggle 4 58
Easy responsive table out of existing table 28 55
ebay style="image-orientation: 90deg;" 2 35
Browser Chrome downloads colored link 5 40
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now