0

I know this is absurd but I am stuck at it

I have a filepath using fileupload in asp classic

The filepath is C:\FakePath\3.jpg

I want to retrieve it in a variable so that it would only give me 3.jpg

substring() and substr() doesn't include 3 I don't know why

logopath = C:\FakePath\3.jpg;
logopath = logopath.substring(10);
2
  • Please post they way you use the substring() or substr() Commented Dec 26, 2012 at 11:57
  • If you want it only with JavaScript please don't use other tags. Commented Dec 27, 2012 at 9:32

5 Answers 5

1

In case you wish to use substring:

var str="C:\\FakePath\\3.jpg";
var imgName = str.substring(12);
Sign up to request clarification or add additional context in comments.

4 Comments

Note that the string would need \\. Also, 12 is needed to strip the last \ as well.
Thanks @pimvdb I realized it after a while.
but there is no \\ only single \
Backslash has a special meaning. Although it is w3Schools, but still : w3schools.com/js/js_obj_string.asp
1

try this

'C:\\FakePath\\3.jpg'.split('\\').pop(); // "3.jpg"

or (regex)

'C:\\FakePath\\3.jpg'.replace(/^.*\\/, '');   // "3.jpg"

enter image description here

4 Comments

there is only single slash
there is only a single slash in your display string. how would you know if \t is backslash t or tab ? in js you should use special chars.
it will always be a backslash not tab ever
Perhaps, play with js and you'll know what @RoyiNamir is talking about. (jsfiddle.net/g3cUK)
1
logopath = encodeURIComponent( logopath ).replace( /.+FakePath%0/, '' )

'\3' is being interpreted as an octal escape sequence which points to a non-printable ASCII character.

Comments

1

Use such code:

function FileChanged(input) {
    var fullPath = input.value;
    var index = fullPath.lastIndexOf("\\");
    var fileName = (index < 0) ? fullPath : fullPath.substr(index + 1);
    alert(fileName);
}​

The two middle lines are what you need: they will take the value after the last slash. This way it doesn't matter what is the path, it will always return only the file name.

Live test case.

Comments

1

If you'd like to solve it in classic ASP, plesae try this.

<%
dim aryPath
aryPath = Split("C:\FakePath\3.jpg","\")
Response.Write aryPath(2)
%>

Hope it could be helpful.

1 Comment

OP asks for JavaScript; note that you can write server-side Classic ASP in either JavaScript or VBScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.