The OpenD Programming Language

baseName

  1. auto baseName(R path)
  2. auto baseName(C[] path)
    baseName
    (
    C
    )
    (
    return scope C[] path
    )
  3. inout(C)[] baseName(inout(C)[] path, C1[] suffix)

Parameters

path C[]

A path name. It can be a string, or any random-access range of characters.

Return Value

Type: auto

The name of the file in the path name, without any leading directory and with an optional suffix chopped off.

If suffix is specified, it will be compared to path using filenameCmp!cs, where cs is an optional template parameter determining whether the comparison is case sensitive or not. See the filenameCmp documentation for details.

Note: This function only strips away the specified suffix, which doesn't necessarily have to represent an extension. To remove the extension from a path, regardless of what the extension is, use stripExtension. To obtain the filename without leading directories and without an extension, combine the functions like this:

assert(baseName(stripExtension("dir/file.ext")) == "file");

Examples

assert(baseName("dir/file.ext") == "file.ext");
assert(baseName("dir/file.ext", ".ext") == "file");
assert(baseName("dir/file.ext", ".xyz") == "file.ext");
assert(baseName("dir/filename", "name") == "file");
assert(baseName("dir/subdir/") == "subdir");

version (Windows)
{
    assert(baseName(`d:file.ext`) == "file.ext");
    assert(baseName(`d:\dir\file.ext`) == "file.ext");
}

Meta

Standards

This function complies with the POSIX requirements for the 'basename' shell utility (with suitable adaptations for Windows paths).