AIR: Listing Files in Directory

by troy on November 9, 2008

Had a small project to compare files in a local system to that of another, thought it would be a fun project to learn more about AIR, only took a couple hours, most research. Here's the relatively simple code to list the files in the directory the AIR app is run in. Learned about that trick from FLEX{er}

import <a href="http://livedocs.adobe.com/apollo/1.0/aslr/flash/filesystem/File.html">flash.filesystem.File</a>;
import flash.events.FileListEvent;
import flash.desktop.NativeApplication;
 
var appDir:File;
var buildDir:File;
 
//debug("url: "+ loaderInfo.url); //app:fileName
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE,onInvoke);
 
function onInvoke(invokeEvent:InvokeEvent):void {
 
	appDir = invokeEvent.currentDirectory;
	path_txt.text = appDir.url;
	buildDir = new File(invokeEvent.currentDirectory.url +"");
 
	buildDir.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListHandler);
	buildDir.getDirectoryListingAsync();
 
}
function dirListHandler(event:FileListEvent):void {
	var contents = event.files;
	var cFile:File;
	for (var i = 0; i < contents.length; i++) {
		cFile = contents[i] as File;
 
		if (cFile.isDirectory) {
			cFile.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListHandler);
			cFile.getDirectoryListingAsync();
 
		} else {
 
                      //show relative paths e.g. something.txt instead of C:\MyApp\something.ttxt
			debug(cFile.url.replace(buildDir.url+"/","") +", " + cFile.size);//+ contents[i].size); 
		}
	}
}
 
 
function debug(str:String):void {
 
	debugTA.htmlText += str;
 
}

For the version living in a webpage, to check the size of the files uploaded, I created a small utility to check if files exist and are the same size via using URLLoader checking the loaded ByteArray.  This isn't as strong as MD5 but is still better than nothing. That FileExistsCheck(er) is here

Leave a Comment

Previous post:

Next post: