Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Remove file path

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
mcarollo1
Asked:
mcarollo1
  • 15
  • 15
1 Solution
 
knightEknightCommented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
knightEknightCommented:
<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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
>> 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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
correct, and I want them to be in the same frame and share a submit button
0
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
yes, that's correct
0
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
The request gommand is not working...
It is seeing the request command as text and filling the box with that
0
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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
 
mcarollo1Author Commented:
It is an IIS server.  And the first page is already an asp connecting sucessfully to the database.
0
 
mcarollo1Author Commented:
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
 
knightEknightCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 15
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now