DirEntries range seems impractical with foreach

Spacen Jasset spacenjasset at mailrazer.com
Mon Mar 17 02:18:35 PDT 2014


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");
         }
     }


More information about the Digitalmars-d-learn mailing list