Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

file_ext

Manipulate filename extensions


Description

Functions to obtain (file_ext()), remove (sans_ext()), and change (with_ext()) extensions in filenames.

Usage

file_ext(x)

sans_ext(x)

with_ext(x, ext)

Arguments

x

A character of file paths.

ext

A vector of new extensions. It must be either of length 1, or the same length as x.

Details

file_ext() is similar to tools::file_ext(), and sans_ext() is similar to tools::file_path_sans_ext(). The main differences are that they treat tar.(gz|bz2|xz) and nb.html as extensions (but functions in the tools package doesn't allow double extensions by default), and allow characters ~ and # to be present at the end of a filename.

Value

A character vector of the same length as x.

Examples

library(xfun)
p = c("abc.doc", "def123.tex", "path/to/foo.Rmd", "backup.ppt~", "pkg.tar.xz")
file_ext(p)
sans_ext(p)
with_ext(p, ".txt")
with_ext(p, c(".ppt", ".sty", ".Rnw", "doc", "zip"))
with_ext(p, "html")

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.