This class aims to parse directory and do something for each file.
You should just override proper methods, then call process method like this.
Here is the simplest example - just print all files under directories.
You should just override proper methods, then call process method like this.
new DirectoryDigger().process("c:/temp");
package utils.file
{
import flash.filesystem.File;
public class DirectoryDigger
{
public function DirectoryDigger()
{
}
public function process(path:String):Object
{
var file:File = new File(path);
preProcess(file);
processBody(file);
return postProcess(file);
}
protected function processBody(file:File):void
{
if (file.isDirectory)
{
preProcessDir(file);
var files : Array = file.getDirectoryListing();
for (var i : int = 0; i < files.length; i++)
{
processBody(files[i] as File);
}
postProcessDir(file);
}
else
{
if(accept(file)){
processFile(file);
}
}
}
// the following method should be overridden if necessary
protected function preProcess(file:File):void {}
protected function postProcess(file:File):Object { return null; }
protected function processFile(file:File) :void {}
protected function preProcessDir(dir:File): void { }
protected function postProcessDir(dir:File): void { }
public function accept(file : File) : Boolean
{
return false;
}
}
}
Here is the simplest example - just print all files under directories.
package utils.tool
{
import flash.filesystem.File;
import utils.file.DirectoryDigger;
public class PrintFileProcessor extends DirectoryDigger
{
public function PrintFileProcessor()
{
}
protected override function preProcess(file:File):void
{
trace("start :" + file.nativePath);
}
override protected function postProcess(file:File):Object
{
trace("finish:" + file.nativePath);
return null;
}
protected override function processFile(file:File):void
{
trace(file.nativePath);
}
}
}
コメント