View source code Display the source code in std/file.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.file.dir_entries - multiple declarations

Function dirEntries

Convenience wrapper for filtering file names with a glob pattern.

Prototype

auto std.algorithm.iteration.FilterResult!(boolstd.file.dirEntries(string,string,std.file.SpanMode,bool).f(std.file.DirEntry),std.file.DirIterator) dirEntries(
  string path,
  string pattern,
  SpanMode mode,
  bool followSymlink = true
);

Parameters

NameDescription
path The directory to iterate over.
pattern String with wildcards, such as "*.d". The supported wildcard strings are described under std.path.globMatch.
mode Whether the directory's sub-directories should be iterated over depth-first (depth), breadth-first (breadth), or not at all (shallow).
Whether symbolic links which point to directories should be treated as directories and their contents iterated over.

Throws

FileException if the directory does not exist.

Examples

// Iterate over all D source files in current directory and all its
// subdirectories
auto dFiles = dirEntries(".","*.{d,di}",SpanMode.depth);
foreach(d; dFiles)
    writeln(d.name);

Function dirEntries

Returns an input range of DirEntry that lazily iterates a given directory, also provides two ways of foreach iteration. The iteration variable can be of type string if only the name is needed, or DirEntry if additional details are needed. The span mode dictates the how the directory is traversed. The name of the each directory entry iterated contains the absolute path.

Prototype

auto std.file.DirIterator dirEntries(
  string path,
  SpanMode mode,
  bool followSymlink = true
);

Parameters

NameDescription
path The directory to iterate over.
mode Whether the directory's sub-directories should be iterated over depth-first (depth), breadth-first (breadth), or not at all (shallow).
Whether symbolic links which point to directories should be treated as directories and their contents iterated over.

Throws

FileException if the directory does not exist.

Examples

// Iterate a directory in depth
foreach (string name; dirEntries("destroy/me", SpanMode.depth))
{
 remove(name);
}
// Iterate a directory in breadth
foreach (string name; dirEntries(".", SpanMode.breadth))
{
 writeln(name);
}
// Iterate a directory and get detailed info about it
foreach (DirEntry e; dirEntries("dmd-testing", SpanMode.breadth))
{
 writeln(e.name, "\t", e.size);
}
// Iterate over all *.d files in current directory and all its subdirectories
auto dFiles = filter!endsWith(a.name,".d")(dirEntries(".",SpanMode.depth));
foreach(d; dFiles)
    writeln(d.name);
// Hook it up with std.parallelism to compile them all in parallel:
foreach(d; parallel(dFiles, 1)) //passes by 1 file to each thread
{
    string cmd = "dmd -c "  ~ d.name;
    writeln(cmd);
    std.process.system(cmd);
}

Example

Duplicate functionality of D1's std.file.listdir():

string[] listdir(string pathname)
{
    import std.file;
    import std.path;
    import std.algorithm;
    import std.array;

    return std.file.dirEntries(pathname, SpanMode.shallow)
        .filter!(a => a.isFile)
        .map!(a => std.path.baseName(a.name))
        .array;
}

void main(string[] args)
{
    import std.stdio;

    string[] files = listdir(args[1]);
    writefln("%s", files);
 }

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis

License

Boost License 1.0.

Comments