0

I have a string name that will generate a file name as paste(name, ".pdf", sep="")

This file will then be moved to another directory after completion.

I would like to proactively avoid any characters in name that might cause errors in file operations like whitespace , \, [], other characters like !"§$%&/()= and any others by replacing them. Special characters in German like öäüß should not cause problems and should be allowed.

Since name is a user input I can't know beforehand what a user might input as character which will then cause problems.

Which characters will give me errors?

Is there any solution or regex to do this without explicitly running each character with sub for example df$name<-sub(" ", "_", df$name)

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
ECII
  • 10,297
  • 18
  • 80
  • 121
  • All of the characters except `/` are legal characters in Linux paths, so it’s unclear what your intent is. – Konrad Rudolph Jan 16 '22 at 13:50
  • This might be what you're looking for https://stackoverflow.com/questions/10294284/remove-all-special-characters-from-a-string-in-r `x <- "a1~!@#$%^&*(){}_+:\"<>?,./;'[]-=" #or whatever str_replace_all(x, "[[:punct:]]", " ")` – thehand0 Jan 16 '22 at 13:51
  • @KonradRudolph I had a space character in name which did not allow the file to be moved via mv – ECII Jan 16 '22 at 13:52
  • 2
    @ECII No, that’s wrong: `mv` handles this just fine (*all* major tools do, with the sole exception of GNU make, which is fundamentally broken). You will need to quote/escape the path, that’s all. (And spaces in particular are relatively common in paths.) – Konrad Rudolph Jan 16 '22 at 13:53
  • @KonradRudolph How can I quote/escape the path? My script combines names and paths with paste() – ECII Jan 16 '22 at 15:57
  • 3
    The R package {fs} provides the function `fs::path_sanitize()`. You probably want to check it out. https://fs.r-lib.org/reference/path_sanitize.html – Seb Jan 16 '22 at 16:40
  • @seb this is exactly what I was looking for. Please post an answer – ECII Jan 16 '22 at 16:54
  • @ECII When using paths as strings in R scripts, your paths are *already quoted* (they’re strings, so they’re already wrapped in quotes), and in fact escaping them further would break them. You only need to do so on the command line/the shell, because it’s the *shell* that otherwise interprets some characters differently: in other words, there are no special characters in paths, but there are special characters *in the shell*. And the way to do this is to simply wrap paths in single quotes, or to escape special characters with a backslash. – Konrad Rudolph Jan 16 '22 at 17:23

1 Answers1

2

The R package {fs} provides the function fs::path_sanitize(). You probably want to check it out. https://fs.r-lib.org/reference/path_sanitize.html

Seb
  • 332
  • 1
  • 3
  • It’s worth noting that this removes *none* of the characters OP objected to, except backslash and (only!) trailing spaces. (Which is good, as far as I’m concerned.) – Konrad Rudolph Jan 17 '22 at 18:39