In D programming, how can I iterate through all files in a folder? Is there a D counterpart to python's glob.iglob?
-
To add on the answers: There are the following SpanMode's: shallow (Which only spans one directory.) depth (Which spans all sub directories before the actual directory.) breadth (Which spans all sub directories after the actual directory.) – Bauss Feb 05 '15 at 13:59
2 Answers
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.

- 25,382
- 4
- 41
- 60
Take a look at std.file.dirEntries
. It will allow you to iterate over all of the files in a directory either shallowly (so it doesn't iterate any subdirectories), with breadth-first search, or with depth-first search. And you can tell it whether you want it to follow symlinks or not. It also supports wildcard strings using std.path.globMatch
. A basic example would be something like
foreach(DirEntry de; dirEntries(myDirectory, SpanMode.shallow))
{
...
}
However, because dirEntries
returns a range of DirEntry
s, it can be used in the various range-based functions in Phobos and not just with foreach
.

- 37,181
- 17
- 72
- 102