View source code Display the source code in std/path.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.path.baseName

Returns the name of a file, 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.

Prototypes

auto baseName(R)(
  R path
)
if (isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) || is(StringTypeOf!R));

inout(C)[] baseName(std.path.CaseSensitive cs, C, C1)(
  inout(C)[] path,
  C1[] suffix
) pure @safe
if (isSomeChar!C && isSomeChar!C1);

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");
}

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");

Standards

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

Authors

Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas Kühne, Andrei Alexandrescu

License

Boost License 1.0

Comments