Path.walk

Iterate over all files and directories inside path;

Produces range with absolute paths found inside specific directory.

Optionally, it is possible to provide glob-patternt, and in this case only paths that match this pattern will be returned.

Note, that, the difference between walk and glob methods is following: walk method applies pattern to absolute path, glob method applies pattern to paths relative to this path.

Warning: system, becuase leads to build error without dip1000 preview flag

  1. auto walk(SpanMode mode, bool followSymlink)
  2. auto walk(string pattern, SpanMode mode, bool followSymlink)
    struct Path
    @system const
    walk
    (
    in string pattern
    ,
    in SpanMode mode = SpanMode.shallow
    ,
    bool followSymlink = true
    )

Parameters

pattern string

The glob pattern to apply to paths inside current dir.

mode SpanMode

The way to traverse directories. See docs

do we need to follow symlinks of not. By default set to True.

Examples

// Iterate over paths in current directory
foreach (p; Path.current.walk(SpanMode.breadth)) {
    if (p.isFile)
        writeln(p);
import dshould;
Path root = createTempPath();
scope(exit) root.remove();

// Create sample directory structure
root.join("d1", "d2").mkdir(true);
root.join("d1", "test1.txt").writeFile("Test 1");
root.join("d1", "d2", "test2.txt").writeFile("Test 2");

// Walk through the derectory d1
Path[] result;
foreach(p; root.join("d1").walk(SpanMode.breadth)) {
    result ~= p;
}

import std.algorithm: sort;
import std.array: array;

result.sort.array.should.equal([
    root.join("d1", "d2"),
    root.join("d1", "d2", "test2.txt"),
    root.join("d1", "test1.txt"),
]);

Walk inside tilda-expandable path

import dshould;
import std.algorithm: startsWith;

// Prepare test dir in user's home directory
Path root = createTempPath("~", "tmp-d-test");
scope(exit) root.remove();

Path hroot = Path("~").join(root.relativeTo(std.path.expandTilde("~")));
hroot._path.startsWith("~").should.be(true);

// Create sample directory structure
hroot.join("d1", "d2").mkdir(true);
hroot.join("d1", "test1.txt").writeFile("Test 1");
hroot.join("d1", "d2", "test2.txt").writeFile("Test 2");

// Walk through the derectory d1
Path[] result;
foreach(p; hroot.join("d1").walk(SpanMode.breadth)) {
    result ~= p;
}

import std.algorithm: sort;
import std.array: array;

result.sort.array.should.equal([
    root.join("d1", "d2"),
    root.join("d1", "d2", "test2.txt"),
    root.join("d1", "test1.txt"),
]);

Meta