On 11/27/2012 01:34 PM, jerro wrote:
On Tuesday, 27 November 2012 at 19:40:56 UTC, Charles Hixson wrote:
Is there a better way to do this? (I want to find files that match any
of some extensions and don't match any of several other strings, or
are not in some directories.):

import std.file;

...

string exts = "*.{txt,utf8,utf-8,TXT,UTF8,UTF-8}";
string[] exclude = ["/template/", "biblio.txt", "categories.txt",
"subjects.txt", "/toCDROM/"]

int limit = 1
// Iterate a directory in depth
foreach (string name; dirEntries(sDir, exts, SpanMode.depth))
{ bool excl = false;
foreach (string part; exclude)
{ if (part in name)
{ excl = true;
break;
}
}
if (excl) break;
etc.

You could replace the inner loop with somehting like:

bool excl = exclude.any!(part => name.canFind(part));

There may be even some easier way to do it, take a look at std.algorithm.

std.algorithm seems to generally be running the match in the opposite direction, if I'm understanding it properly. (Dealing with D template is always confusing to me.) OTOH, I couldn't find the string any method, so I'm not really sure what you're proposing, though it does look attractive.

Still, though your basic approach sounds good, the suggestion of Joshua Niehus would let me filter out the strings that didn't fit before entering the loop. There's probably no real advantage to doing it that way, but it does seem more elegant. (You were right, though. That is in std.algorithms.)

Reply via email to