Does AS3 have a built in class / function to extract "filename" from a complete path. e.g. I wish to extract "filename.doc" from full path "C:\Documents and Settings\All Users\Desktop\filename.doc"
Couldn't you just do something basic like:
string filename = filename.substring(filename.lastIndexOf("\\") + 1)
I know it's not a single function call, but it should work just the same.
Edited based on @Bryan Grezeszak's comment.
Apparently you can use the File class, or more specifically, the File.separator static member if you're working with AIR. It should return "/" or "\", which you can plug in to @cmptrgeekken's suggestion.
This regex works with both the \ and / notation:
var filename:String = fullPath.match(/(?:\\|\/)([^\\\/]*)$/)[1];
First you want to find the last occurrence of / or \ in the path, do that using this:
var fSlash: int = fullPath.lastIndexOf("/");
var bSlash: int = fullPath.lastIndexOf("\\"); // reason for the double slash is just to escape the slash so it doesn't escape the quote!!!
var slashIndex: int = fSlash > bSlash ? fSlash : bSlash;
That will give you the index in the string that is right BEFORE that last slash. So then to return the portion of the string after that, you add one to the index (moving it past the last slash) and return the remainder of the string
var docName: String = fullPath.substr(slashIndex + 1);
To do this as a simple to use function, do this:
function getFileName(fullPath: String) : String
{
var fSlash: int = fullPath.lastIndexOf("/");
var bSlash: int = fullPath.lastIndexOf("\\"); // reason for the double slash is just to escape the slash so it doesn't escape the quote!!!
var slashIndex: int = fSlash > bSlash ? fSlash : bSlash;
return fullPath.substr(slashIndex + 1);
}
var fName: String = getFileName(myFullPath);