windows 8.1 and dirEntries

Ali Çehreli acehreli at yahoo.com
Thu Dec 26 15:55:06 PST 2013


On 12/26/2013 07:36 AM, Stephen Jones wrote:

 > Code that was fine on 32 bit XP is ow crashing Windows 8.1:

Actually, it is not a crash. The program is telling us that something it 
was asked to do cannot be accomplished. A function throws an exception, 
nobody catches it, and the program ends due to an unhandled exception.

 > foreach(string s; dirEntries(directory, SpanMode.shallow)){
 >    if(endsWith(s, "~")) continue;
 >    try{
 >      if(isDir(s)) d ~= s;
 >    }catch(FileException e){   }finally{}
 >    try{
 >      if(isFile(s)){
 >        if(endsWith(s, sufix)){
 >          f ~= s;
 >        }
 >      }
 >    }catch(FileException e){}finally{}
 > }
 >
 > The error I receive is:
 >
 > Bypasses std.file.FileException at std\file.d(2262)
 > ===Bypassed===
 > std.fileFileException at std\file.d(2262): c:\User\Administrator\: Access
 > Denied

I think the user who is running the program does not have access rights 
to open Administrator's directory. (The rest of my post assumes that it 
is an access right issue.)

 > wouldn't any problem there be caught in the try .. catch block?
 > And how come the try .. catch block is being bypassed?

Maybe the exception is being thrown during the foreach iteration, 
outside of your two try-catch blocks. I would try putting a try-catch 
block around the entire foreach statement.

If that works and if skipping the problematic entries is an option, use 
an explicit while (or for) loop so that you can catch it more precisely:

import std.stdio;
import std.file;

void main()
{
     auto dirRange = dirEntries("/tmp/deneme", SpanMode.shallow);

     while (!dirRange.empty) {
         writeln(dirRange.front);
         dirRange.popFront();
     }
}

Now you can put a try-catch around individual range operations: 
construction, empty, front, and popFront().

Ali



More information about the Digitalmars-d-learn mailing list