iterate over a directory, dealing with permission errors

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 18 04:35:43 PDT 2015


Posting here instead of learn because I think it uncovers a 
design flaw

void main(string[] args)
{
     import std.file : dirEntries, SpanMode;
     import std.stdio : writeln;
     foreach(file; dirEntries(args[1], SpanMode.depth))
         writeln(file.name);
}

Modify this program such that it will print "<file.name> access 
denied" instead of crashing with an exception whenever it hits a 
permissions problem. Remember that you might not even have 
permission to read the directory given in args[1]. Remember that 
access permissions can change at any time.

It can be done, but it is seriously ugly.


More information about the Digitalmars-d mailing list