How to skip permission denied exceptions if iterate through directories?

Clas Onnebrink clas at gmx.net
Sat Jan 25 03:52:27 PST 2014


On Saturday, 25 January 2014 at 02:26:20 UTC, Rikki Cattermole 
wrote:
> On Friday, 24 January 2014 at 23:46:04 UTC, Clas Onnebrink 
> wrote:
>>
>> Hi,
>>
>> Im a newbie in D and I have a small problem. Thats my method
>>
>> //*********************************
>>   private void searchAndRunAction() {
>>      foreach (entry;dirEntries(paramPath,
>>                                paramSearchType,
>>                                (paramSwitchRecursiveOn ? 
>> SpanMode.breadth : SpanMode.shallow)))
>>      {
>>         filesCount++;
>>
>>         if (isDuplicate(entry.name)) {
>>            filesDupCount++;
>>            if (paramSwitchDeleteOn)
>>               remove(entry.name);
>>         }
>>
>>         printProgress(entry.name);
>>      }
>>   }
>>
>> I want work through a directory on my linux server but there 
>> are some
>> directories I have no permissions to access so I get following:
>>
>> ~/Projects/cltools/smdups $ source/smdups -r 
>> -p=/media/clas/Elements2 -e=*.*
>> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353): 
>> /media/clas/Elements2/lost+found: Permission denied
>> ----------------
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x43cb1d]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x43eab4]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x404768]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x404116]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x404d2d]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x41a71f]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x41ae7f]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x41b0b0]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x41ae7f]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x41b018]
>> /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) 
>> [0x7f66a29c8de5]
>> /home/clas/Projects/cltools/smdups/source/smdups() [0x403e63]
>> ----------------
>> ~/Projects/cltools/smdups $ 
>> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353): 
>> /media/clas/Elements2/lost+found: Permission denied
>> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353):: 
>> command not found
>>
>> My question: How to skip any exceptions in dirEntries. I tried 
>> it with filter.
>> But no chance. Is there a way to do it like in C# with 
>> LINQ-Expressions?
>>
>> greets
>>
>> clas
>
> One way would just be to catch the exceptions. Note you may 
> want to activate debug mode to know what functions are actually 
> failing.
> A FileException is being fired when it cannot do a file 
> operation as detailed in phobos documentation.

To catch the exception dont solve it. Because if dirEntries 
throws an exception then it doesnt going on through the 
directories. I need dirEntries step over directories where I have 
no permissions whithout raise exceptions.


More information about the Digitalmars-d-learn mailing list