Get the extension of a vector of filenames

Share:

Description

Get the extension of a vector of filenames, assuming that the extension is the set of characters that follows the last ".". A wrapper for grabLast.

Usage

1
getExtension(vec, split.char = ".")

Arguments

vec

Character vector (usually containing filenames)

split.char

A single character used to split the character strings

Details

Assumes paths are delineated using forward slashes. If an NA is supplied, then an NA is returned. If the desired string doesn't exist (see examples below), a "" is returned.

Value

Character vector of filename extensions

Author(s)

Landon Sego

See Also

Additional functions for filename manipulations: stripExtension, stripPath, getPath, grabLast, basename, dirname

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
getExtension(c(a = "this old file.doc",
               b = "that young file.rtf",
               c = "this.good.file.doc",
               d = "this_bad_file",
               e = "thisfile.",
               f = NA,
               g = "that.this.pdf",
               h = ".", i = ""))

# An example with 'real' files
files <- dir(file.path(path.package(package = "Smisc"), "data"), full.names = TRUE)
print(files)
getExtension(files)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.