Learn R Programming

nat.utils (version 0.6.1)

common_path: Find common prefix of two or more (normalised) file paths

Description

Find common prefix of two or more (normalised) file paths

Usage

common_path(paths, normalise = FALSE, fsep = .Platform$file.sep)

Value

Character vector of common prefix, "" when there is no common prefix, or the original value of paths when fewer than 2 paths were supplied.

Arguments

paths

Character vector of file paths

normalise

Whether to normalise paths (with normalizePath, default FALSE)

fsep

Optional path separator (defaults to .Platform$file.sep)

Details

Note that for absolute paths, the common prefix will be returned e.g. common_path(c("/a","/b")) is "/"

Note that normalizePath 1) operates according to the conventions of the current runtime platform 2) is called with winslash=.Platform$file.sep which means that normalised paths will eventually end up separated by "\" by default on Windows rather than by "//", which is normalizePath's standard behaviour.

See Also

normalizePath

Other path_utils: abs2rel(), split_path()

Examples

Run this code
common_path(c("/a","/b"))
common_path(c("/a/b/","/a/b"))
common_path(c("/a/b/d","/a/b/c/d"))
common_path(c("/a/b/d","/b/c/d"))
common_path(c("a","b"))
common_path(c("","/a"))
common_path(c("~","~/"))
common_path(c("~/a/b/d","~/a/b/c/d"), normalise = FALSE)
common_path(c("~","~/"), normalise = FALSE)

Run the code above in your browser using DataLab