0

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.

Wicowan
  • 13
  • 5
  • Maybe could you replace the strings with unicode. For example, "é" is written "\u00e9" in unicode. You can find other ones here https://www.compart.com/fr/unicode/U+00E9 – Yacine Hajji Jan 13 '22 at 15:53
  • @But the thing is that the problem doesn't come from the replacement, but to find the original path name. it seems the list.files() doesn't give the exact file name that R wants when renaming a file... – Wicowan Jan 13 '22 at 16:52

2 Answers2

0

For the string conversion problem, instead of your long list of str_replace_all() calls, you could probably simply use:

newfilename <- iconv(str, to = 'ASCII//TRANSLIT')

For the renaming, as @defuneste pointed out, you added twice the path, instead you should just do:

file.rename(file.path(Path, iconv(my_list[i], to = 'ASCII//TRANSLIT'))
Karl Forner
  • 4,175
  • 25
  • 32
  • Oh I wanted to use the iconv function but when I wanted to download the package, it said it was in another version of R. i didn't know it was now in R – Wicowan Jan 13 '22 at 16:27
  • And just tested it, it actually doesn't work, é becomes e? and â becomes a^ – Wicowan Jan 13 '22 at 16:35
  • Didn't see you edit, anyway I get this error message for everything I try : Warning message: In file.rename(file.path(list[i]), iconv(list[i], to = "ASCII//TRANSLIT")) : cannot rename file 'G:/qgis/Batiment_PDF_MACRO/Batiments_Communaux/Bex/Bex Chemin Pre´-de-la-Cible 4' to 'G:/qgis/Batiment_PDF_MACRO/Batiments_Communaux/Bex/Bex Chemin Pre?-de-la-Cible 4', reason 'The system cannot find the file specified' – Wicowan Jan 13 '22 at 17:17
  • weird, just tested copying your â from this page: iconv("â", to = 'ASCII//TRANSLIT') --> "a" – Karl Forner Jan 13 '22 at 17:45
  • yep normal bc this is a normal â, try this "â", it will output a^ – Wicowan Jan 13 '22 at 17:57
  • try this "é" also, it will output e? – Wicowan Jan 13 '22 at 18:03
  • yep --> "e", on R 3.6.3, on linux, locale() --> Encoding: UTF-8 – Karl Forner Jan 13 '22 at 18:14
0

I think you need to add the path see below.

library(stringr)

#dummy data
Path <- "test/"
dir.create("test")
file.create("test/désolé")
file.create("test/purée")

# I dont like naming an object with a function name 
# You need the path or it failed see full.names
my_list <- list.files(path = Path, full.names = T) 

# here we go !
for (i in 1:length(my_list)) {
str_replace_all(my_list[i], "é", "e")
file.rename(my_list[i], str_replace_all(my_list[i], "é", "e"))
}

list.files("test/")

Here a second version I used @karl-forner iconv() and try with a part of one of your path. It works but it seems we can't reproduce your trouble ...

Path <- "test/"
dir.create("test")
file.create("test/désolé")
file.create("test/purée")
file.create("test/Bex Chemin Pre´-de-la-Cible 4")


# I dont like namking an object with a function name 
# You need the path or it failed see full.names
my_list <- list.files(path = Path, full.names = T) 
my_list

# here we go !
for (i in 1:length(my_list)) {

file.rename(my_list[i], iconv(my_list[i], to = 'ASCII//TRANSLIT'))
}

list.files("test/")


defuneste
  • 376
  • 2
  • 7
  • This doesn't work, still the same problem, the path can't be found – Wicowan Jan 13 '22 at 16:39
  • Can you test my code ? You will maybe need to adapt it a bit to your operating system (ie change / to \) . If my code works we can advance a bit on your issue. – defuneste Jan 13 '22 at 16:50
  • my bad this actually works with your "dummy data", but it seems my file name are strange, see my edit for more info – Wicowan Jan 13 '22 at 16:54
  • I do not have a good solution.... Can you try some `iconv("château", toRaw = TRUE)` ? My guess is that you have multiple encoding ... – defuneste Jan 13 '22 at 17:19
  • it outputs me this : 63 68 e2 74 65 61 75, kinda strange – Wicowan Jan 13 '22 at 17:23
  • The output is normal â is 0xe2. I am out of idea exept trying encoding() on some path (see examples in encoding help) .. – defuneste Jan 13 '22 at 20:05