DirEntries range seems impractical with foreach

anonymous anonymous at example.com
Mon Mar 17 13:39:44 PDT 2014


On Monday, 17 March 2014 at 09:18:36 UTC, Spacen Jasset wrote:
> While trying to use dirEntries range with foreach I encountered 
> a seemingly unsurmountable problem. The problem is that an 
> execption is thrown while calling .front() on the range that 
> dirEntries returnes, which seems to mean it's impossible to use 
> it with a for loop if you exepect any sort of 'access denied' 
> or other file exception - which will be quite common. e.g. in 
> c:\windows
>
> Is there any way to fix the foreach loop version (a)
>
>
> (a) Can't make this work
>  foreach (DirEntry e; entries) {
>
>     }
> std.file.FileException at std\file.d(2262): c:/windows\CSC\v2.0.6: 
> Access is denied
>
>
> (b)This works
>     auto entries = dirEntries(path, spanMode);
>     while (!entries.empty) {
>         try {
>             DirEntry e = entries.front();
>             //...
>             entries.popFront();
>         } catch (FileException e) {
>             writeln("Skipping something");
>         }
>     }

b doesn't work in the general case, either. dirEntries throws
immediately when the very first entry is not accessible.


More information about the Digitalmars-d-learn mailing list