Path.parent

Determine parent path of this path.

Note, by default, if path is not absolute, then it will be automatically converted to absolute path, before getting parent path.

If parameter absolute is set to false, then base path will not be converted to absolute path before computing. On attempt to get parent out of scope, the Path(".") will be returned. For example: Path("test").parent(false) == Path(".") Path("test", "..").parent(false) == Path(".") Path("test", "..", "..").parent(false) == Path(".") Path("test", "test2").parent(false) == Path("test")

struct Path
const
parent
(
in bool absolute = true
)

Parameters

absolute bool

covert path to absolute (if needed) before computing parent path.

Return Value

Type: Path

Path to parent directory.

Examples

import dshould;

version(Posix) {
    Path("/tmp").parent.toString.should.equal("/");
    Path("/").parent.toString.should.equal("/");
    Path("/tmp/parent/child").parent.toString.should.equal("/tmp/parent");

    Path("parent/child").parent.toString.should.equal(
        Path(std.file.getcwd).join("parent").toString);

    auto cdir = std.file.getcwd;
    scope(exit) std.file.chdir(cdir);

    std.file.chdir("/tmp");

    version(OSX) {
        Path("parent/child").parent.toString.should.equal(
            "/private/tmp/parent");
    } else {
        Path("parent/child").parent.toString.should.equal(
            "/tmp/parent");
    }

    Path("~/test-dir").parent.toString.should.equal(
        "~".expandTilde);
}

Compute parent path without converting to absolute path

import dshould;

Path("tmp", "dir").parent(false).should.equal(Path("tmp"));
Path("tmp").parent(false).should.equal(Path("."));

Path("tmp").parent(false).parent(false).should.equal(Path("."));
Path("").parent(false).parent(false).should.equal(Path("."));
Path("test").parent(false).should.equal(Path("."));
Path("test",  "..").parent(false).should.equal(Path("."));
Path("test", "..", "..").parent(false).should.equal(Path("."));
Path("test",  "test2").parent(false).should.equal(Path("test"));
Path("test",  "test2").parent(false).join("test3").should.equal(
    Path("test", "test3"));

Meta