Path.glob

Search files that match provided glob pattern inside current path.

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.

struct Path
@system
glob
(
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.

Return Value

Type: auto

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

Examples

1 import dshould;
2 import std.array: array;
3 import std.algorithm: sort;
4 Path root = createTempPath();
5 scope(exit) root.remove();
6 
7 // Create sample directory structure
8 root.join("d1").mkdir(true);
9 root.join("d1", "d2").mkdir(true);
10 root.join("d1", "test1.txt").writeFile("Test 1");
11 root.join("d1", "test2.txt").writeFile("Test 2");
12 root.join("d1", "test3.py").writeFile("print('Test 3')");
13 root.join("d1", "d2", "test4.py").writeFile("print('Test 4')");
14 root.join("d1", "d2", "test5.py").writeFile("print('Test 5')");
15 root.join("d1", "d2", "test6.txt").writeFile("print('Test 6')");
16 
17 // Find py files in directory d1
18 root.join("d1").glob("*.py").array.should.equal([
19     root.join("d1", "test3.py"),
20 ]);
21 
22 // Find py files in directory d1 recursively
23 root.join("d1").glob("*.py", SpanMode.breadth).array.sort.array.should.equal([
24     root.join("d1", "d2", "test4.py"),
25     root.join("d1", "d2", "test5.py"),
26     root.join("d1", "test3.py"),
27 ]);
28 
29 // This will match .py files inside d1 directory and d2 directory
30 root.glob("d*/*.py", SpanMode.breadth).array.sort.array.should.equal([
31     root.join("d1", "d2", "test4.py"),
32     root.join("d1", "d2", "test5.py"),
33     root.join("d1", "test3.py"),
34 ]);
35 
36 // Find py files in directory d1 recursively
37 root.join("d1").glob("*.txt", SpanMode.breadth).array.sort.array.should.equal([
38     root.join("d1", "d2", "test6.txt"),
39     root.join("d1", "test1.txt"),
40     root.join("d1", "test2.txt"),
41 ]);
42 
43 const auto current = Path.current;
44 scope(exit) current.chdir;
45 
46 root.join("d1").chdir;
47 version(OSX) {
48     Path(".").glob("*.txt", SpanMode.breadth).array.sort.array.should.equal([
49         root.realPath.join("d1", "d2", "test6.txt"),
50         root.realPath.join("d1", "test1.txt"),
51         root.realPath.join("d1", "test2.txt"),
52     ]);
53 } else {
54     Path(".").glob("*.txt", SpanMode.breadth).array.sort.array.should.equal([
55         root.join("d1", "d2", "test6.txt"),
56         root.join("d1", "test1.txt"),
57         root.join("d1", "test2.txt"),
58     ]);
59 }

Meta