Function std.path.baseName
auto baseName(R)
(
R path
)
if (isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) && !isSomeString!R);
auto baseName(C)
(
C[] path
)
if (isSomeChar!C);
inout(C)[] baseName(CaseSensitive cs = CaseSensitive .osDefault, C, C1)
(
inout(C)[] path,
in C1[] suffix
) pure @safe
if (isSomeChar!C && isSomeChar!C1);
Parameters
Name | Description |
---|---|
cs | Whether or not suffix matching is case-sensitive. |
path | A path name. It can be a string, or any random-access range of characters. |
suffix | An optional suffix to be removed from the file name. |
Returns
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");
Standards
This function complies with the POSIX requirements for the 'basename' shell utility (with suitable adaptations for Windows paths).
Example
writeln(baseName("dir/file.ext")); // "file.ext"
writeln(baseName("dir/file.ext", ".ext")); // "file"
writeln(baseName("dir/file.ext", ".xyz")); // "file.ext"
writeln(baseName("dir/filename", "name")); // "file"
writeln(baseName("dir/subdir/")); // "subdir"
version (Windows)
{
writeln(baseName(`d:file.ext`)); // "file.ext"
writeln(baseName(`d:\dir\file.ext`)); // "file.ext"
}
Authors
Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas Kühne, Andrei Alexandrescu