File Names and Extensions
Every path has a IPath.Name: the final segment of the path. File paths additionally split that name into a base portion and an extension. The library exposes helpers to read those parts and to derive new file paths with a different extension, while always validating the result against the path's PathFormat and PathOptions.
Reading the Name
IPath.Name returns the final segment for any path:
FilePath.ParseAbsolute(@"C:\Apps\MyApp\data.json").Name; // "data.json"
DirectoryPath.ParseAbsolute(@"C:\Apps\MyApp").Name; // "MyApp"
A directory's name does not include the trailing separator that its IPath.PathDisplay ends with. For roots, Name returns the root segment (e.g. "C:" for a Windows drive root or "/" on Unix).
Single Extensions and Multi-Extensions
A file name doesn't say where its extension starts: backup.tar.gz has the extension .tar.gz, but my.report.v2.pdf has the extension .pdf. The file path members come in two families for that reason, and the member names say which family a member belongs to.
Single extension (members named …Extension): Extension, NameWithoutExtension, HasExtension, WithExtension, AddExtension and RemoveExtension work with the last extension, the part of the name from the last dot. The four that take an extension (HasExtension, WithExtension, AddExtension and RemoveExtension) take one with a single part (.gz, not .tar.gz). A successful call keeps Extension accurate: for any non-empty extension x they accept, path.WithExtension(x).Extension and path.AddExtension(x).Extension are x, path.HasExtension(x) is true exactly when path.Extension equals x ignoring case, and path.RemoveExtension(x) succeeds only when path.HasExtension(x) is true.
Multi-extension (members named …MultiExtension): HasMultiExtension, GetMultiExtension, AddMultiExtension and RemoveMultiExtension work with a multi-extension, which has one or more parts (.gz or .tar.gz). For any non-empty extension x they accept, path.AddMultiExtension(x) gives a path whose HasMultiExtension(x) is true and whose GetMultiExtension(x) returns x, and RemoveMultiExtension(x) on that path gives back path. With an extension of one part, they give the same results as the single extension members.
HasExtension, RemoveExtension, HasMultiExtension, GetMultiExtension and RemoveMultiExtension ignore case on every platform, so photo.JPG has the extension .jpg on Unix too. The extension rules say which extensions each family takes.
File Name Without Extension
IFilePath.NameWithoutExtension returns the file name with the trailing extension and its dot removed:
FilePath.ParseRelative("report.pdf").NameWithoutExtension; // "report"
FilePath.ParseRelative("archive.tar.gz").NameWithoutExtension; // "archive.tar"
FilePath.ParseRelative("Makefile").NameWithoutExtension; // "Makefile"
Only the last dot delimits the extension. A file name like archive.tar.gz has extension .gz and base name archive.tar.
Extension
IFilePath.Extension returns the extension including the leading dot, or an empty string if the file has none:
FilePath.ParseRelative("report.pdf").Extension; // ".pdf"
FilePath.ParseRelative("archive.tar.gz").Extension; // ".gz"
FilePath.ParseRelative("Makefile").Extension; // ""
FilePath.ParseRelative(".gitignore").Extension; // ".gitignore"
Note
The last-dot rule has two edge cases. A name whose only dot is the leading one, such as .gitignore, is all extension: IFilePath.Extension returns the whole name and IFilePath.NameWithoutExtension returns an empty string. A file name that ends with a dot (only possible when parsing with PathOptions.None) has extension "."; the trailing dot is preserved verbatim and treated as the extension.
HasExtension: Check
IFilePath.HasExtension tells whether the last extension is the given single extension, ignoring case:
var archive = FilePath.ParseRelative("backups/archive.TAR.GZ");
archive.HasExtension(".gz"); // true
archive.HasExtension(".tar"); // false: not the last extension
archive.HasExtension(".tar.gz"); // ArgumentException: more than one part (see HasMultiExtension)
archive.HasExtension(""); // ArgumentException: the extension is empty
A name that consists of the extension has it: .gitignore has the extension .gitignore, as Extension also gives. Extension is an empty string for a name without an extension.
WithExtension: Replace
IFilePath.WithExtension returns a new path with the last extension replaced. The new extension must be empty/null or start with a single . and contain no further dots.
var report = FilePath.ParseAbsolute(@"C:\out\report.pdf");
report.WithExtension(".html"); // C:\out\report.html
report.WithExtension(null); // C:\out\report (extension removed)
report.WithExtension(".tar.gz"); // ArgumentException: more than one part
For multi-extension files, only the last extension is replaced:
var archive = FilePath.ParseRelative("backups/archive.tar.gz");
archive.WithExtension(".bz2"); // backups/archive.tar.bz2
archive.WithExtension(null); // backups/archive.tar
AddExtension: Append
IFilePath.AddExtension appends a single extension without removing any existing one:
var src = FilePath.ParseRelative("backups/archive.tar");
src.AddExtension(".gz"); // backups/archive.tar.gz
src.AddExtension(".tar.gz"); // ArgumentException: more than one part (see AddMultiExtension)
A null or empty extension leaves the path unchanged. The extension becomes the last extension, so path.AddExtension(x).Extension is x for any non-empty extension it accepts.
RemoveExtension: Strip
IFilePath.RemoveExtension returns a new path with a single extension removed from the end of the file name, ignoring case:
var archive = FilePath.ParseAbsolute(@"C:\out\archive.TAR.GZ");
archive.RemoveExtension(".gz"); // C:\out\archive.TAR
archive.RemoveExtension(".zip"); // ArgumentException: the name doesn't end with the extension
archive.RemoveExtension(".tar.gz"); // ArgumentException: more than one part (see RemoveMultiExtension)
archive.RemoveExtension(""); // C:\out\archive.TAR.GZ (unchanged)
It throws ArgumentException if the name doesn't end with the extension, if the extension is the whole name (a file named .gitignore), or if the remaining name is invalid for the supplied PathOptions (see Validation).
HasMultiExtension: Check
IFilePath.HasMultiExtension tells whether the file name ends with a multi-extension, ignoring case:
var archive = FilePath.ParseRelative("backups/archive.TAR.GZ");
archive.HasMultiExtension(".tar.gz"); // true
archive.HasMultiExtension(".gz"); // true
archive.HasMultiExtension(".tar"); // false
archive.HasMultiExtension(""); // ArgumentException: the extension is empty
A name that consists of the extension ends with it: .gitignore has the multi-extension .gitignore, and .tar.gz has the multi-extension .tar.gz.
GetMultiExtension: Match
IFilePath.GetMultiExtension gives the longest of the known multi-extensions that the file name ends with, as it was passed in, otherwise Extension:
FilePath.ParseRelative("backup.tar.gz").GetMultiExtension(".tar.gz", ".tar.bz2"); // ".tar.gz"
FilePath.ParseRelative("BACKUP.TAR.GZ").GetMultiExtension(".tar.gz", ".tar.bz2"); // ".tar.gz"
FilePath.ParseRelative("photo.jpg").GetMultiExtension(".tar.gz", ".tar.bz2"); // ".jpg"
FilePath.ParseRelative("Makefile").GetMultiExtension(".tar.gz", ".tar.bz2"); // ""
FilePath.ParseRelative("backup.tar.gz").GetMultiExtension(".gz", ".tar.gz"); // ".tar.gz" (the longest match)
Its result can be passed to RemoveMultiExtension, which leaves a name without an extension unchanged:
var file = FilePath.ParseRelative("backups/archive.tar.gz");
file.RemoveMultiExtension(file.GetMultiExtension(".tar.gz", ".tar.bz2")); // backups/archive
For a name that is all extension, such as .gitignore, RemoveMultiExtension throws, because no name would remain.
AddMultiExtension: Append
IFilePath.AddMultiExtension appends a multi-extension without removing any existing one:
var data = FilePath.ParseRelative("backups/archive");
data.AddMultiExtension(".tar.gz"); // backups/archive.tar.gz
data.AddMultiExtension(".gz"); // backups/archive.gz
A null or empty extension leaves the path unchanged. The new name ends with the extension, so path.AddMultiExtension(x).HasMultiExtension(x) is true for any non-empty extension it accepts.
RemoveMultiExtension: Strip
IFilePath.RemoveMultiExtension returns a new path with a multi-extension removed from the end of the file name, ignoring case:
var archive = FilePath.ParseAbsolute(@"C:\out\archive.TAR.GZ");
archive.RemoveMultiExtension(".tar.gz"); // C:\out\archive
archive.RemoveMultiExtension(".gz"); // C:\out\archive.TAR
archive.RemoveMultiExtension(".zip"); // ArgumentException: the name doesn't end with the extension
archive.RemoveMultiExtension(""); // C:\out\archive.TAR.GZ (unchanged)
It throws ArgumentException if the name doesn't end with the extension, if the extension is the whole name (a file named .tar.gz), or if the remaining name is invalid for the supplied PathOptions (see Validation).
Extension Rules
A single extension starts with a dot and has no other dots: .gz is accepted, gz, .tar.gz and .. are not. HasExtension, WithExtension, AddExtension and RemoveExtension take single extensions and throw ArgumentException for any other shape, including a multi-extension with several parts.
A multi-extension starts with a dot, and every dot is followed by a character other than a dot: .gz and .tar.gz are accepted, gz, ..gz, .tar..gz and .tar. are not. HasMultiExtension, GetMultiExtension, AddMultiExtension and RemoveMultiExtension take multi-extensions and throw ArgumentException for any other shape.
A lone dot . is accepted by both families, since that is the Extension of a name that ends with a dot. Characters that names can't have in the path format (such as ? on Windows) are rejected. The extensions each family takes are the ones for which format.IsValidExtension(extension, PathOptions.None) (single) or format.IsValidMultiExtension(extension, PathOptions.None) (multi) returns true; the members that make a new name also check it with the options they are given (see Validation).
An empty extension:
- leaves the path unchanged in AddExtension, AddMultiExtension, RemoveExtension and RemoveMultiExtension;
- removes the last extension in WithExtension;
- throws in HasExtension, HasMultiExtension and GetMultiExtension.
Validation
WithExtension, AddExtension, AddMultiExtension, RemoveExtension and RemoveMultiExtension validate the new file name against the supplied PathOptions (default PathOptions.NoUnfriendlyNames). The rest of the path is not re-parsed.
var path = FilePath.ParseRelative("data");
path.WithExtension(".txt "); // throws: new name "data.txt " ends with a space
path.WithExtension("."); // throws: new name "data." ends with a dot
path.WithExtension(".txt ", PathOptions.None); // "data.txt " (validation relaxed)
The new name also can't be . or .., which would make the path a directory path.
PathFormat.IsValidExtension checks a single extension, the kind the single extension members take:
PathFormat.Universal.IsValidExtension(".tar.gz"); // false: multiple dots
PathFormat.Universal.IsValidExtension("gz"); // false: no leading dot
PathFormat.Universal.IsValidExtension(".gz"); // true
PathFormat.Universal.IsValidExtension(""); // true: empty is valid
PathFormat.IsValidMultiExtension checks a multi-extension, checking each part as IsValidExtension checks a single extension with the given options (PathOptions.NoUnfriendlyNames when none are given):
PathFormat.Universal.IsValidMultiExtension(".tar.gz"); // true
PathFormat.Universal.IsValidMultiExtension(".tar .gz"); // false: ".tar " ends with a space
PathFormat.Universal.IsValidMultiExtension(".tar .gz", PathOptions.None); // true
PathFormat.Universal.IsValidMultiExtension(".tar..gz"); // false: empty part
Renaming
The library does not expose a "rename" method on file paths directly. Renaming is just combining the parent directory with a new file name:
var oldPath = FilePath.ParseAbsolute(@"C:\out\report.pdf");
var newPath = oldPath.ParentDirectory.CombineFile("report-final.pdf");
oldPath.MoveTo(newPath);
This pattern composes nicely with extension changes:
var temp = FilePath.ParseAbsolute(@"C:\out\report.pdf.tmp");
var final = temp.WithExtension(null); // strip ".tmp"
temp.MoveTo(final);
Return Types
WithExtension, AddExtension, AddMultiExtension, RemoveExtension and RemoveMultiExtension preserve the static type of the path: calling them on IAbsoluteFilePath returns IAbsoluteFilePath, and on IRelativeFilePath returns IRelativeFilePath. No casts required.
Next Steps
These guides pick up where renaming leaves off:
- Combining and Navigating Paths: combine works hand-in-hand with renaming.
- Working with Files: apply renames with IAbsoluteFilePath.MoveTo.