Directory recursive walking

drug drug2004 at bk.ru
Thu Jan 14 15:55:24 UTC 2021


On 1/14/21 6:46 PM, dog2002 wrote:
> I need to make some operations with all the files in a directory and 
> subdirectories. Currently, I do it like this:
> 
> import std;
> 
> void DirIteration(string path) {
>      try {
>          foreach(entry; dirEntries(path, SpanMode.shallow, false)) { 
> //SpanMode.shallow allows skip directories if any error happens
>              if (entry.isFile && !entry.isSymlink)
>                  writeln(entry); //Or something instead of this
>              if (entry.isDir)
>                  DirIteration(entry);
>          }
>      }
>      catch (Throwable) {}
> }
> 
> void main()
> {
>      DirIteration("C:\\Users\\angrypuppy\\MyDir");
> }
> 
> But this method consumes a huge amount of memory (up to 4 GB and more). 
> Is there a more appropriate way to walk directories recursively that 
> does not consume a lot of memory?

DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


More information about the Digitalmars-d-learn mailing list