www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - iterate over a directory, dealing with permission errors

reply Daniel Pflager <daniel.pflager gmail.com> writes:
https://forum.dlang.org/post/pldneecaysxueshzwopx forum.dlang.org

On Sunday, 23 May 2021 at 15:19:34 UTC, xray wrote:
 On Sunday, 23 May 2021 at 15:13:31 UTC, Mike Parker wrote:
 On Sunday, 23 May 2021 at 15:12:22 UTC, Mike Parker wrote:

 Are you calling it with `RangePrimitive.pop` or 
 `RangePrimitive.popFront`?
I meant, `RangePrimitive.front` or `RangePrimitive.popFront`.
RangePrimitive.popFront In SpanMode.depth I still get the FileException and in SpanMode.breadth, the program hangs for ever.
I've got a test environment set up. DMD32 D Compiler v2.098.0-dirty Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved written by Walter Bright Windows 10 Enterprise (1909) OS Build 18363.2037 Test program (fixit.d): ``` import std.exception; import std.file; import std.stdio; int main(string[] args) { // Use glob to search by glob pattern foreach (string name; dirEntries(args[1], SpanMode.breadth, false).handle!(FileException, RangePrimitive.popFront, (e, r) => DirEntry())) { writeln(name); } return 0; } ``` Using git bash shell GNU bash, version 4.4.23(1)-release (x86_64-pc-msys) ``` time ./fixit.exe C:\\ ``` Results in ... ``` C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc C:\$Recycle.Bin\S-1-5-21-2025429265-1303643608-1417001333-1255143\$RTQTCU0.1-javadoc . . . ```
Jan 21 2022
parent Paul Backus <snarwin gmail.com> writes:
On Saturday, 22 January 2022 at 02:09:39 UTC, Daniel Pflager 
wrote:
 https://forum.dlang.org/post/pldneecaysxueshzwopx forum.dlang.org

 On Sunday, 23 May 2021 at 15:19:34 UTC, xray wrote:
 On Sunday, 23 May 2021 at 15:13:31 UTC, Mike Parker wrote:
 On Sunday, 23 May 2021 at 15:12:22 UTC, Mike Parker wrote:

 Are you calling it with `RangePrimitive.pop` or 
 `RangePrimitive.popFront`?
I meant, `RangePrimitive.front` or `RangePrimitive.popFront`.
RangePrimitive.popFront In SpanMode.depth I still get the FileException and in SpanMode.breadth, the program hangs for ever.
[...] Test program (fixit.d): ``` import std.exception; import std.file; import std.stdio; int main(string[] args) { // Use glob to search by glob pattern foreach (string name; dirEntries(args[1], SpanMode.breadth, false).handle!(FileException, RangePrimitive.popFront, (e, r) => DirEntry())) { writeln(name); } return 0; } ```
Unfortunately, there is no easy workaround for this. Because the exception is thrown by `popFront`, the front of the range can never actually be popped, and iteration cannot advance beyond the problematic element. Catching the exception and retrying `popFront` will just give the exact same error again, as you've seen. Probably the best you can do (without implementing your own `dirEntries` iterator from scratch) is to use `SpanMode.shallow` to iterate over each individual directory with a separate loop. A permission error will still force you to skip the directory it occurs in, but you will be able to continue on to other files and directories adjacent to it.
Jan 21 2022