遍历 D 中文件夹中的文件

Iterating through files in a folder in D

在D编程中,如何遍历文件夹中的所有文件? python's glob.iglob有对应的D吗?

看看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。一个基本的例子是

foreach(DirEntry de; dirEntries(myDirectory, SpanMode.shallow))
{
    ...
}

但是,由于 dirEntries returns 范围 DirEntrys,它可以用于 Phobos 中的各种基于范围的功能,而不仅仅是 foreach .

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

很喜欢

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

有关详细信息,请参阅文档。第二个字符串 *.txt 过滤器是可选的,如果您将其省略,您将看到所有文件。

SpanMode 可以很浅,可以跳过进入子文件夹或类似 SpanMode.depth 的方式进入子文件夹。