Search code examples
d

Iterating through files in a folder in D


In D programming, how can I iterate through all files in a folder? Is there a D counterpart to python's glob.iglob?


Solution

  • http://dlang.org/phobos/std_file.html#dirEntries

    So like

    import std.file;
    foreach(string filename; dirEntries("folder_name", "*.txt", SpanMode.shallow) {
         // do something with filename
    }
    

    See the docs for more info. The second string, the *.txt filter, is optional, if you leave it out, you see all files.

    The SpanMode can be shallow to skip going into subfolders or something like SpanMode.depth to descend into them.