Path.glob

Search files that match provided glob pattern inside current path.

struct Path
@safe
glob
(
in string pattern
,
in SpanMode mode = SpanMode.shallow
,
bool followSymlink = true
,
bool relative = false
)

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.

Return Value

Type: auto

Range of absolute path inside specified directory, that match specified glob pattern.

Examples

import dshould;
import std.array: array;
import std.algorithm: sort;
Path root = createTempPath();
scope(exit) root.remove();

// Create sample directory structure
root.join("d1").mkdir(true);
root.join("d1", "d2").mkdir(true);
root.join("d1", "test1.txt").writeFile("Test 1");
root.join("d1", "test2.txt").writeFile("Test 2");
root.join("d1", "test3.py").writeFile("print('Test 3')");
root.join("d1", "d2", "test4.py").writeFile("print('Test 4')");
root.join("d1", "d2", "test5.py").writeFile("print('Test 5')");
root.join("d1", "d2", "test6.txt").writeFile("print('Test 6')");

// Find py files in directory d1
root.join("d1").glob("*.py").array.should.equal([
    root.join("d1", "test3.py"),
]);

// Find py files in directory d1 recursively
root.join("d1").glob("*.py", SpanMode.breadth).array.should.equal([
    root.join("d1", "test3.py"),
    root.join("d1", "d2", "test4.py"),
    root.join("d1", "d2", "test5.py"),
]);

// Find py files in directory d1 recursively
root.join("d1").glob("*.txt", SpanMode.breadth).array.sort.array.should.equal([
    root.join("d1", "d2", "test6.txt"),
    root.join("d1", "test1.txt"),
    root.join("d1", "test2.txt"),
]);

Meta