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

path_sanitize

Sanitize a filename by removing directory paths and invalid characters


Description

path_sanitize() removes the following:

  • Control characters

  • Reserved characters

  • Unix reserved filenames (. and ..)

  • Trailing periods and spaces (invalid on Windows)

  • Windows reserved filenames (CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9) The resulting string is then truncated to 255 bytes in length

Usage

path_sanitize(filename, replacement = "")

Arguments

filename

A character vector to be sanitized.

replacement

A character vector used to replace invalid characters.

See Also

https://www.npmjs.com/package/sanitize-filename, upon which this function is based.

Examples

# potentially unsafe string
str <- "~/.\u0001ssh/authorized_keys"
path_sanitize(str)

path_sanitize("..")

fs

Cross-Platform File System Operations Based on 'libuv'

v1.5.0
GPL-3
Authors
Jim Hester [aut, cre], Hadley Wickham [aut], libuv project contributors [cph] (libuv library), Joyent, Inc. and other Node contributors [cph] (libuv library), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

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