clean_file_name: Clean a file name string from illegal characters

Description Usage Arguments Details Value Examples

View source: R/main.R

Description

Make a string used as part of a file path legal on a file system by removing illegal characters. The defaults should give a good portable result for most modern file systems.

Usage

1
2
3
4
5
6
7
clean_file_name(
  inf = "file name",
  suffix = "",
  blacklist = paste("[^", paste(LETTERS, sep = "", collapse = ""), paste(letters, sep =
    "", collapse = ""), paste(0:9, sep = "", collapse = ""), "\\s_&\\.\\-]", sep =
    "", collapse = "")
)

Arguments

inf

A character vector to be cleaned into a file name component.

suffix

A suffix to be attached if any, e.g. .PDF or .document.

blacklist

A regular expression character class used as a blacklist, i.e. the negated white list of characters. It's a very safe set, please use what you feel is safe in your circumstances.

The default safe characters are:

  • upper case letters and

  • lower case letters and

  • numbers 0:9 and

  • the dot separator, i.e. full stop and

  • the underscore and

  • the hyphen and

  • the ampersand and

  • the space

Details

File systems tolerate certain characters in a file name. Some characters are not legal. This function will replace all characters that are not white listed with space. Multiple consecutive spaces will be replaced with a single one and the leading and trailing spaces will be stripped.

The cleaning is not comprehensive. It deals with characters only. For example, P, R and N are perfectly legal in Windows but a file named PRN might not be very useful. However, for example, it will strip out | (a pipe) as an illegal character. Linux and others allow a broader character sets in file names than Windows, but the point of this function is to create a safe portable name.

Value

Cleaned file name component.

Examples

1
clean_file_name(c("fred123", "some file", "bad & good##", "w##H$A%t^"))

mickmioduszewski/framerrr documentation built on Oct. 19, 2020, 3:58 a.m.