Show
Ignore:
Timestamp:
03/17/09 07:37:11 (4 years ago)
Author:
moriyoshi
Message:

* Implementing glob-matching option.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/wshutils/trunk/WSHUtils/DirectoryWalker.js

    r31299 r31301  
    3232 
    3333WSHUtils.DirectoryWalker.prototype = { 
    34     initialize: function(path) { 
     34    initialize: function(path, options) { 
     35        if (typeof options == 'undefined') { 
     36            options     = { 
     37                files: '*', 
     38                excludes: [] 
     39            }; 
     40        } 
     41 
     42        var ex_matcher = []; 
     43        for (var i = options.excludes.length; --i >= 0;) { 
     44            ex_matcher[i] = new WSHUtils.GlobMatcher(options.excludes[i]); 
     45        } 
     46 
    3547        this.queue = [ FileSystemObject.GetFolder(path) ]; 
     48        this.matcher = new WSHUtils.GlobMatcher(options.files); 
     49        this.ex_matcher = ex_matcher; 
    3650        this.currentFolder = null; 
    3751        this.current = null; 
     
    5064 
    5165    item: function() { 
    52         if (this.current == null) { 
    53             if (this.queue.length > 0) { 
    54                 this.currentFolder = this.queue.pop(); 
    55                 WScript.Echo(this.currentFolder); 
    56                 for (var i = new Enumerator(this.currentFolder.SubFolders); 
    57                         !i.atEnd(); i.moveNext()) { 
    58                     this.queue.push(i.item());  
     66        for (;; this.current.moveNext()) { 
     67            if (this.current == null || this.current.atEnd()) { 
     68                if (this.queue.length > 0) { 
     69                    this.currentFolder = this.queue.pop(); 
     70                    for (var i = new Enumerator(this.currentFolder.SubFolders); 
     71                            !i.atEnd(); i.moveNext()) { 
     72                        var folder = i.item(); 
     73                        if (!this.matcher.match(folder.Name)) 
     74                            continue; 
     75                        var j = this.ex_matcher.length; 
     76                        while (--j >= 0) { 
     77                            if (this.ex_matcher[j].match(folder.Name))  
     78                                break; 
     79                        } 
     80                        if (j >= 0) 
     81                            continue; 
     82                        this.queue.push(folder);  
     83                    } 
     84                    this.current = new Enumerator(this.currentFolder.Files); 
     85                } else { 
     86                    return null; 
    5987                } 
    60                 this.current = new Enumerator(this.currentFolder.Files); 
    6188            } 
     89 
     90            if (this.current.atEnd()) 
     91                continue; 
     92 
     93            var file = this.current.item(); 
     94            if (!this.matcher.match(file.Name)) 
     95                continue; 
     96            var j = this.ex_matcher.length; 
     97            while (--j >= 0) { 
     98                if (this.ex_matcher[j].match(file.Name))  
     99                    break; 
     100            } 
     101            if (j >= 0) 
     102                continue; 
     103 
     104            return file; 
    62105        } 
    63         return this.current != null ? this.current.item(): null; 
    64106    } 
    65107};