path matching problem

Joshua Niehus jm.niehus at gmail.com
Tue Nov 27 13:31:36 PST 2012


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.

maybe this:?

import std.algorithm, std.array, std.regex;
import std.stdio, std.file;
void main()
{
     enum string[] exts  =  [`".txt"`, `".utf8"`, `".utf-8"`, 
`".TXT"`, `".UTF8"`, `".UTF-8"`];
     enum string exclude = 
`r"/template/|biblio\.txt|categories\.txt|subjects\.txt|/toCDROM/"`;

     auto x = dirEntries("/path", SpanMode.depth)
         .filter!(`endsWith(a.name,` ~ exts.join(",") ~ `)`)
         .filter!(`std.regex.match(a.name,` ~ exclude ~ 
`).empty`);;

     writeln(x);
}


More information about the Digitalmars-d-learn mailing list