Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename files in R

I have problem with renaming files from R.

In my folder on Desktop there are 10 files:

račun 1.xlsx

račun 2.xlsx

...

račun 10.xlsx

I have tried the following:

files <- list.files(path = "myfolder")
file.rename(files,
        paste0("novi_", 1:10, ".xlsx"))

This is what I get as an outcome:

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

I suppose this is because of unicode character č, but I do not know how to find a solution for this.

like image 246
Luka Avatar asked Nov 24 '25 20:11

Luka


1 Answers

EDIT 2: The solution here was for the OP to change the Region settings in Control Panel, setting format to be in Serbian(Latin, Serbia).

EDIT 1: See the comments: the OP is on a Windows machine. Here the problem is that list.files() (and presumably dir(), since they call the same .Internal) is converting the non ASCII filenames to ASCII, but Windows is expecting file.exists() to send it the unicode filenames, (and presumably also file.rename())

Try:

file.rename(gsub("c", "č", files), paste0("novi_", seq_along(files, ".xlsx"))
# could work, but it didn't for `file.exists()`

Original answer:

setwd(<your path>)
(files <- list.files())
# [1] "račun 1.xlsx" "račun 2.xlsx" "račun 3.xlsx" "račun 4.xlsx" "račun 5.xlsx [6] "račun 6.xlsx"    
file.rename(files, paste0("novi_", seq_along(files, ".xlsx"))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE

The fact that you specified a path in list.files(), suggests that you're not in the correct directory

like image 74
De Novo Avatar answered Nov 27 '25 08:11

De Novo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!