Solved

Remove file path

Posted on 2000-04-19
30
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Expert Comment

by:knightEknight
Comment Utility
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
Comment Utility
yes, that's correct
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It is an IIS server.  And the first page is already an asp connecting sucessfully to the database.
0
 

Author Comment

by:mcarollo1
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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 style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

772 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

12 Experts available now in Live!

Get 1:1 Help Now