[R] recursively rename dir and files

maxbre mbressan at arpa.veneto.it
Thu Apr 9 21:58:32 CEST 2015

this is my reproducible example

dir.create("./my dir with space")
dir.create("./my dir with space/my sub dir with space")
file.create("./my dir with space/my dir file with space.txt")
file.create("./my dir with space/my sub dir with space/my sub dir file with

now I need to rename recursively all dirs and files in order to get rid of

the following attempt is not getting to the point...

mylist<-dir(".", full.names=TRUE, recursive=TRUE, include.dirs=TRUE)

for (i in mylist){
  file.rename(i, gsub(" ", "_", i))

#or more simply...
file.rename(mydirs, gsub(" ", "_", mydirs))

...because (clearly) I got some warning messages like "can not rename file
.... because it is not existing";
and I definitely understand that because in the process of renaming of the
the upper level directory the full path of the nested directories and files
is changed and any longer visible to the procedure...

the problem now is that I'm not enough clear how to proceed with an
alternative strategy in order to properly sort out the problem...

for reasons I'm not mentioning here I must stick with a solution in R

any hint much appreciated


View this message in context: http://r.789695.n4.nabble.com/recursively-rename-dir-and-files-tp4705667.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list