Learn R Programming

this.path (version 2.5.0)

basename2: Manipulate File Paths

Description

basename2() removes all of the path up to and including the last path separator (if any).

dirname2() returns the part of the path up to but excluding the last path separator, or "." if there is no path separator.

Usage

basename2(path)
dirname2(path)

Value

A character vector of the same length as path.

Arguments

path

character vector, containing path names.

Behaviour on Windows

If path is an empty string, then both dirname2() and basename2() return an emty string.

\ and / are accepted as path separators, and dirname2() does NOT translate the path separators.

Recall that a network share looks like "//host/share" and a drive looks like "d:".

For a path which starts with a network share or drive, the path specification is the portion of the string immediately afterward, e.g. "/path/to/file" is the path specification of "//host/share/path/to/file" and "d:/path/to/file". For a path which does not start with a network share or drive, the path specification is the entire string.

The path specification of a network share will always be empty or absolute, but the path specification of a drive does not have to be, e.g. "d:file" is a valid path despite the fact that the path specification does not start with "/".

If the path specification of path is empty or is "/", then dirname2() will return path and basename2() will return an empty string.

Behaviour under Unix-alikes

If path is an empty string, then both dirname2() and basename2() return an emty string.

Recall that a network share looks like "//host/share".

For a path which starts with a network share, the path specification is the portion of the string immediately afterward, e.g. "/path/to/file" is the path specification of "//host/share/path/to/file". For a path which does not start with a network share, the path specification is the entire string.

If the path specification of path is empty or is "/", then dirname2() will return path and basename2() will return an empty string.

Details

Tilde-expansion (see ?path.expand()) of the path will be performed.

Trailing path separators are removed before dissecting the path, and for dirname2() any trailing file separators are removed from the result.

Examples

Run this code
path <- c("/usr/lib", "/usr/", "usr", "/", ".", "..")
x <- cbind(path, dirname = dirname2(path), basename = basename2(path))
print(x, quote = FALSE, print.gap = 3)

Run the code above in your browser using DataLab