Skip to content

ddn.os.path

This page provides runnable examples for common filesystem path tasks using the ddn.os.path module.


1) Build and inspect paths

#!/usr/bin/env dub
/+ dub.sdl:
    name "os-path-basics"
    dependency "ddn" version="*"
+/
module examples.os_path_basics;

import std.stdio : writeln, writefln;
import ddn.os.path;

void main() {
   auto p = Path("logs") / "2025" / "app.log";

   writeln("p        = ", p.str());
   writeln("name     = ", p.name());
   writeln("stem     = ", p.stem());
   writeln("suffix   = ", p.suffix());
   writeln("parent   = ", p.parent().str());
   writeln("absolute = ", p.absolute().str());

   // Parents() yields the chain of parent directories.
   writeln("parents:");
   foreach (par; p.parents())
      writeln("  ", par.str());
}

2) Create files/directories and glob for matches

This example creates a temporary directory structure, writes a few files, and uses glob/rglob.

#!/usr/bin/env dub
/+ dub.sdl:
    name "os-path-glob"
    dependency "ddn" version="*"
+/
module examples.os_path_glob;

import std.stdio : writeln;
import std.file : tempDir, rmdirRecurse, write;
import std.path : buildPath;
import ddn.os.path;

void main() {
   auto base = Path(buildPath(tempDir(), "ddn-os-path-example"));
   if (base.exists())
      rmdirRecurse(base.str());
   scope (exit) {
      if (base.exists())
         rmdirRecurse(base.str());
   }

   base.mkdir();
   (base / "a.txt").touch();
   (base / "b.md").touch();

   auto sub = base / "sub";
   sub.mkdir();
   write((sub / "c.txt").str(), "hello\n");

   writeln("glob(\"*.txt\"):");
   foreach (p; base.glob("*.txt"))
      writeln("  ", p.str());

   writeln("glob(\"**/*.txt\"):");
   foreach (p; base.glob("**/*.txt"))
      writeln("  ", p.str());

   writeln("rglob(\"c.txt\"):");
   foreach (p; base.rglob("c.txt"))
      writeln("  ", p.str());
}