Extracting "filename" from the full path in actionscript 3

Does AS3 have a built-in class / function to extract the "filename" from the full path. for example I want to extract " filename .doc " from the full path "C: \ Documents and Settings \ All Users \ Desktop \ filename.doc"

+5
source share
6 answers

For Air, you can try using the File Class to extract the file name.

var file:File=new File("path_string");   
//path_string example "C:\\Windows\\myfile.txt"  
var filename:String = file.name;
+9
source

Could you just do something basic:

string filename = filename.substring(filename.lastIndexOf("\\") + 1)

I know that this is not a single function call, but it should work the same.

Edited based on a comment by @Bryan Grezeszak.

+4
source

/\ , :

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;

, . , , ( )

var docName: String = fullPath.substr(slashIndex + 1);

, :

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);
+4

-, File, , , File.separator, AIR. "/" "\", @cmptrgeekken.

+2

:

var file_ :File = new File("C:/Usea_/Dtop/sinim (1).jpg"); // or url variable ... whatever//

file_ = file_.parent;

trace(file_.url);
0

- :

var tmpArray:Array<String>;
var fileName:String;

tmpArray = fullFilePath.split("\");
fileName = tmpArray.pop();

You need to make sure that you use the Unix file system ("/") or the Windows file system ("\").

0
source

All Articles