Click to See Complete Forum and Search --> : filename from path


barcsdad
11-25-2003, 02:16 PM
I'm having no luck in extracting the filename from a path such as "c:\folder1\folder2\filename.jpg". I think its the backslashes that are giving me problems. Any ideas would be appreciated.

96turnerri
11-25-2003, 02:30 PM
have you tried "file:///c:\...." or is it file:// not sure one will work

fredmv
11-25-2003, 03:12 PM
I would imagine it's because the backslashes are being treated as escapes. You basically need to escape the escapes.

c:\\folder1\\folder2\\filename.jpg

barcsdad
11-26-2003, 08:10 AM
I realize that escaping the backslashes works, but if I'm getting the path from a file upload field, how would I go about doing that? What I'm ultimately trying to do is to make sure the file that is being upload only contains alpha-numeric characters. I've got a check for that, but my script is checking the entire path (i.e. "c:\documents and settings\folder\myfile.jpg"), instead of just the filename (i.e. "myfile.jpg"). How can I get it to only check the actual filename?

----------------------------------------------------------------

<html>
<head>
<title></title>
<script>
function valbutton(thisform) {
var pattern = /^[a-zA-Z0-9 ]+$/;
if(!thisform.image.value.match(pattern)){
alert('Filename can only contain alpha-numeric characters and no spaces.\nPlease rename your file and try again.');
thisform.image.focus();
return false;
}
thisform.submit();
}

</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="myform">
<input type="file" name="image"><br><br>
<input type="button" name="submit" value="upload" onclick="valbutton(myform)">
</form>

</body>
</html>

ray326
11-26-2003, 07:57 PM
This will pull it apart for you but you'll need to pre-process the incoming string, properly escaping the '\' chars.

"c:\\folder1\\folder2\\filename.jpg".match(/(^.*)\\(.*)/)