Accessing the contents of a folder
AIR allows you to obtain a list of files and folders in a particular folder. This can be very useful if, for example, you want to create an application to load and display all images in a given folder. For each file and folder, you can also access detailed information such as the size, the extension (if it’s a file), the remaining disk room, the creation date, and so on.To access the list of files and documents in a folder, you have to use the getDirectoryListing() function provided by the File class. This method returns a list of File objects contained in the relevant folder.
In this exemple I will use a “DataGrid” to be easy to see what we have in the folder, a “TextArea” to see the location of the folder, and of course a button. Well, this is code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package { import fl.controls.Button; import fl.controls.DataGrid; import fl.controls.TextArea; import fl.controls.dataGridClasses.DataGridColumn; import fl.data.DataProvider; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.filesystem.File; public class Main extends MovieClip { public var choose:Button; public var output:TextArea; public var directoryList:DataGrid; private var folder:File; public function Main() { super(); choose.addEventListener( MouseEvent.CLICK, showDirectorySelection ); var col1:DataGridColumn = new DataGridColumn(); var col2:DataGridColumn = new DataGridColumn(); var col3:DataGridColumn = new DataGridColumn(); var col4:DataGridColumn = new DataGridColumn(); col1.headerText = "File name"; col1.dataField = "name"; directoryList.addColumn( col1 ); col2.headerText = "File size"; col2.dataField = "size"; directoryList.addColumn( col2 ); col3.headerText = "File extension"; col3.dataField = "extension"; directoryList.addColumn( col3 ); col4.headerText = "Creation date"; col4.dataField = "creationDate"; directoryList.addColumn( col4 ); } private function showDirectorySelection( event:MouseEvent ):void { folder = new File(); folder.addEventListener( Event.SELECT, directorySelected ); folder.browseForDirectory("Choose a directory on your system"); } private function directorySelected( event:Event ):void { output.appendText("Selected folder: "+folder.nativePath+"\n"); var directoryContents:Array = folder.getDirectoryListing(); var dp:DataProvider = new DataProvider(); var folderNum:int = -1; var item:File; for each( item in directoryContents ) { if( item.isDirectory ) { folderNum++; dp.addItemAt( item, folderNum ); } else { dp.addItem( item ); } } directoryList.dataProvider = dp; } } } |
And you can download the final AIR file by here: Accessing the contents of a folder

November 17th, 2009 at 1:06 AM
Hey, how do you get the creation date from a File?
November 17th, 2009 at 2:20 PM
Hello Patrick,
At line 47, I have : col4.dataField = “creationDate”; . This line of the code give me the creation date of the file.
Horia
February 17th, 2010 at 5:09 PM
Будет ли продолжение этой темы?