Path.relativeTo

Return this path as relative to base

  1. Path relativeTo(Path base)
    struct Path
    const
    relativeTo
    ()
  2. Path relativeTo(string base)

Parameters

base Path

base path to make this path relative to. Must be absolute.

Return Value

Type: Path

new Path that is relative to base but represent same location as this path.

Throws

PathException if base path is not valid or not absolute

Examples

import dshould;
Path("foo").relativeTo(std.file.getcwd).toString().should.equal("foo");

version(Posix) {
    auto path1 = Path("/foo/root/child/subchild");
    auto root1 = Path("/foo/root");
    auto root2 = Path("/moo/root");
    auto rpath1 = path1.relativeTo(root1);

    rpath1.toString.should.equal("child/subchild");
    root2.join(rpath1).toString.should.equal("/moo/root/child/subchild");
    path1.relativeTo(root2).toString.should.equal("../../foo/root/child/subchild");

    // Base path must be absolute, so this should throw error
    Path("~/my/path/1").relativeTo("~/my").should.throwA!PathException;
}

Meta