This may be a duplicate, but I haven't found an answer on other posts so I'll post it right here. I have problem renaming files that have an accent (in French). My goal is to delete all accents in all files and then rename them with the appropriate name. I think the problem comes from file.list(). For example when I list a file which name is "désolé", it will be listed as "de´sole´". But surprisingly I can still replace the "e´" as an "é" with str_replace_all(). The problem comes when I finally try to rename the file. I can't even access the file because my path is not read correctly, because I take the changed name from the file list, I can't select the file, thus can't rename it. here's the code :
library(stringr)
Path <- "G:/qgis/Batiment_PDF_MACRO/Batiments_Communaux/Bex/"
list <- list.files(path=Path)
for (i in 1:length(list)) {
pattern <- c("é", "è", "ê", "ë", "à", "â", "ä", "î", "ï", "ö", "ô", "û", "ü", "ù", "ç")
if (str_contains(list[i], pattern, logic = "OR") == TRUE) {
filename <- paste0(Path, list[i])
name <- str_replace_all(list[i], "é", "e")
name <- str_replace_all(name, "è", "e")
name <- str_replace_all(name, "ê", "e")
name <- str_replace_all(name, "ë", "e")
name <- str_replace_all(name, "à", "a")
name <- str_replace_all(name, "â", "a")
name <- str_replace_all(name, "ä", "a")
name <- str_replace_all(name, "ï", "i")
name <- str_replace_all(name, "î", "i")
name <- str_replace_all(name, "ö", "o")
name <- str_replace_all(name, "ô", "o")
name <- str_replace_all(name, "û", "u")
name <- str_replace_all(name, "ü", "u")
name <- str_replace_all(name, "ù", "u")
name <- str_replace_all(name, "ç", "c")
newfilename <-paste0(Path, name)
file.rename(filename, newfilename)
}
}
I tried to change the encoding and stuff but with no success...
[EDIT] Actually I think my folder name are written in strange characters or I dunno. For example I just noticed that some letters such as "â" don't get replaced, I tried all these :
name <- str_replace_all(name, "â", "a")
name <- str_replace_all(name, "a^", "a")
name <- str_replace_all(name, "^", "")
But none work, but If I set myself
name <- "château"
Here the output is successfully "chateau" which means my folder names are somehow strange. The folder were also created with R with the dir.create() function and the names come from picture names which were originally named in an apple computer (mac) and I own a pc, maybe the problem comes from there ?
[EDIT OF EDIT] I went to see on this site (https://unicodelookup.com/) what actual characters my folder names were made of and this is very strange. If you or I type "é" on this site you will get this : "é : latin small letter e with acute" BUT when I copy paste the "é" from my folder, this happens : "e : latin small letter e" + "´ : combining acute accent". That means that the é from my folde names are different from the normal é, the list.files() function probably transform these strange é into normal é, thus why I can't find the right path anymore.