regex - Most efficient way to look for files in a directory tree that match a regular expression -


i writing little app enable users specify regular expression specify files copy location. expression might this:

\w\:\/rootfolderone\/subfoldertwo/\w+/filename\d+.mpg 

so question how efficiently find files on drive match pattern?

i have load contents of each folder 1 one. traverse directory tree , load every folder in whole file system , compare each file's entire path regular expression. wasteful loading contents of rootfoldertwo or subfolderthree not provide valid results.

at root level how know load rootfolderone not rootfoldertwo?

most efficient way? don't know in air. maybe ane you'd better result. pure air though, can think of:

(it slow, using worker thread highly advised)

    private var expression:regexp;  //to store regex search     private var matchedfiles:vector.<file>; //store matched files      public function main()      {         expression = /\btest(er | ing | ed | s)?\b/; //anything word test/tester/testing/tests in          matchedfiles = new vector.<file>();         //scanfolder(new file("file:///c://")); //windows c drive,  replace whatever approprite camera card path.         scanfolder(file.documentsdirectory); //for testing purposes i'm going scan documents directory          trace(matchedfiles.length + " files found");     }      private function scanfolder(file:file):void {         if (!file.exists) return; //if directory doesn't exist, exit function          var files:array = file.getdirectorylisting(); //list of files in directory         var f:file;         (var i:int = 0; < files.length; i++ ) { //loop thorugh files             f = files[i];             if (f.isdirectory) {                 scanfolder(f); //if directory, scan             }else{                 //if matches regex, put matched array                 if (expression.test(f.name)) matchedfiles.push(f);             }         }     } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -