prune with dirEntries

Jonathan M Davis jmdavisProg at gmx.com
Thu Nov 29 18:30:48 PST 2012


On Friday, November 30, 2012 02:57:20 Dan wrote:
> On Friday, 30 November 2012 at 01:13:13 UTC, Jonathan M Davis
> 
> wrote:
> > On Friday, November 30, 2012 01:24:07 Dan wrote:
> >> Is there a way to walk files with std.file.dirEntries such that
> >> certain directories are skipped (i.e. how to avoid .git
> >> entirely/recursively)?
> > 
> > You can use std.algorithm.filter on its result. Then when it
> > would iterate to
> > something which doesn't match filter's predicate, it skips it.
> > 
> > - Jonathan M Davis
> 
> That will do the filtering correctly - but what I was hoping was
> to actually prune at the directory level and not drill down to
> the files in of an unwanted directory (e.g. .git). The problem
> with this and what I'm trying to overcome is accessing lots of
> files and directories recursively all of which I want to skip.
> Much like there is a *followSymlink* it would be nice if a
> predicate were accepted to *followDirectory* in general or some
> way to cause that.
> 
> ---------------
> 
> static bool desired(string m) {
> bool unwanted = match(m, _uninterestRe)? true : false;
> writeln("Is unwanted ", m, " ", unwanted);
> return !unwanted;
> }
> static Regex!(char) _uninterestRe = regex(`\.git\b`);
> filter!(desired)(dirEntries(root, SpanMode.depth))) {
> ...
> }

You can use the glob matching overload then:

auto dirEntries(string path, string pattern, SpanMode mode,
 bool followSymlink = true)

I don't really know how to use it though, so you'll have to read the docs and 
figure it out.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list