Код: Выделить всё
package{ import flash.display.Sprite; import flash.events.*; import flash.filesystem.*; import flash.desktop.*; import flash.net.SharedObject; import flash.text.TextField; import flash.text.TextFieldAutoSize; public class SearchFiles extends Sprite { private var file:File; private var findFilesArr:Vector.<File>; private var countDirectories:int; private var so:SharedObject; // расширения файлов для поиска private var fileTypesArr:Array = [".jpg", ".gif", ".png"]; private var textField:TextField; public function SearchFiles():void { stage ? init() : addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // типа кнопка ) textField = new TextField; textField.autoSize = TextFieldAutoSize.CENTER; textField.border = true; textField.htmlText = '<p align="center"><font size="16"> поиск </font></p>'; textField.x = stage.stageWidth / 2 - textField.width / 2; textField.y = stage.stageHeight / 2 - textField.height / 2; textField.selectable = false; textField.addEventListener(MouseEvent.CLICK, onTextFieldClick); addChild(textField); so = SharedObject.getLocal("remember_path"); so.data.path == undefined ? so.data.path = File.desktopDirectory.nativePath : null; // добавляем метод к массиву addMethodsTo(Array, {in_array: function(what:*):Boolean { for (var a:int = 0; a < this.length; a++) { if (this[a] == what) { return true; } } return false; }}); } private function onTextFieldClick(e:MouseEvent):void { file = new File; file = file.resolvePath(so.data.path); file.addEventListener(Event.SELECT, onFolderSelected); file.browseForDirectory("Выберите папку"); } private function onFolderSelected(e:Event):void { // создаем массив для найденных файлов findFilesArr = new Vector.<File>; // запоминаем последний путь поиска so.data.path = file.nativePath; // запускаем асинхронный поиск file.addEventListener(FileListEvent.DIRECTORY_LISTING, browseDirectory); file.addEventListener(IOErrorEvent.IO_ERROR, onFileIOError); file.getDirectoryListingAsync(); textField.htmlText = '<p align="center"><font size="16"> идет поиск файлов </font></p>'; textField.mouseEnabled = false; } private function browseDirectory(e:FileListEvent):void { countDirectories--; (e.currentTarget as File).removeEventListener(FileListEvent.DIRECTORY_LISTING, browseDirectory); (e.currentTarget as File).removeEventListener(IOErrorEvent.IO_ERROR, onFileIOError); for each (var f:File in e.files) { if (f.isDirectory == true) { // если директория, ищем в ней countDirectories++; f.addEventListener(FileListEvent.DIRECTORY_LISTING, browseDirectory); f.addEventListener(IOErrorEvent.IO_ERROR, onFileIOError); f.getDirectoryListingAsync(); } else { if (fileTypesArr.in_array(f.type)) { findFilesArr.push(f); } } } if (countDirectories <= 0) { trace("Поиск завершен. "); if (findFilesArr.length == 0) { trace("Ничего не найдено. "); } else { for (var i:int = 0; i < findFilesArr.length; i++) { trace(File(findFilesArr[i]).nativePath); } } textField.mouseEnabled = true; textField.htmlText = '<p align="center"><font size="16"> поиск </font></p>'; } } private function onFileIOError(e:IOErrorEvent):void { trace(e.toString()); } // создание прототипов private function addMethodsTo(cls:Class, methods:Object):void { for (var str:String in methods) { cls.prototype[str] = methods[str]; cls.prototype.setPropertyIsEnumerable(str, false); } } } }