this is my reproducible example setwd(".") 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 space.txt") now I need to rename recursively all dirs and files in order to get rid of spaces 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 language any hint much appreciated thanks -- 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.
Hi maxbre, Try this: recursive_replace<-function(path=".",replace=" ",with="_") { filelist<-list.files(path,full.names=TRUE) for(filename in filelist) { if(length(grep(replace,filename))) file.rename(filename,gsub(replace,with,filename)) } dirlist<-list.dirs(path,full.names=TRUE,recursive=FALSE) if(length(dirlist)) { for(dirname in dirlist) recursive_replace(dirname,replace=replace,with=with) } } Jim On Fri, Apr 10, 2015 at 5:58 AM, maxbre <mbressan at arpa.veneto.it> wrote:> this is my reproducible example > > setwd(".") > 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 > space.txt") > > now I need to rename recursively all dirs and files in order to get rid of > spaces > > 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 > language > > any hint much appreciated > > thanks > > > > > > > -- > 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Thu, Apr 9, 2015 at 2:58 PM, maxbre <mbressan at arpa.veneto.it> wrote:> this is my reproducible example > > setwd(".") > 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 > space.txt") > > now I need to rename recursively all dirs and files in order to get rid of > spaces > > 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 > language > > any hint much appreciated > > thanks > >?You need to rename each file in each subdirectory. But you must first split the file name from the directory name. You then rename the file, keeping the directory name the same. In order for this to work, you must first rename all files in a given directory _before_ attempting to rename the directory. Once each file in a directory is renamed, then you can try to rename the directory itself. I.e. you must process the files in "depth" order. The following code does that. filesAndDirs=dir(".", full.names=TRUE, recursive=TRUE, include.dirs=TRUE) hasBlanks=filesAndDirs[grepl(" ",filesAndDirs)] #only things with blanks, please sortedBlankList=sort(hasBlanks,decreasing=TRUE) for(file in sortedBlankList) { front=dirname(file) back=basename(file) newBack=gsub(" ","_",back) if(newBack != back) { ? #actually need to do a rename? newFile=file.path(front,newBack) file.rename(file,newFile) } }? ?BTW - neat turban. Is it Sikh? Hope you don't mind my curiosity.? -- If you sent twitter messages while exploring, are you on a textpedition? He's about as useful as a wax frying pan. 10 to the 12th power microphones = 1 Megaphone Maranatha! <>< John McKown [[alternative HTML version deleted]]
hi jim thank you very much for your help: what a nice piece of code! inspired by your neat solution let me here post a slight variation upon your lines which is now also dealing also with caps in file and dir names (a potentially useful function for the proper "housekeeping" of the wd) it works but there is one potentially "dangerous" drawback (to be somehow controlled): it renames the R code file eventually present in the wd which is usually named something like *.R which is renamed as *.r I definitely need to think a viable solution to avoid that problem... #### setwd(".") dir.create("./My Dir with Spaces and Caps") dir.create("./My Dir with Spaces and Caps/My Sub Dir With Spaces and Caps") file.create("./My Dir with Spaces and Caps/My Dir file With Spaces and Caps.txt") file.create("./My Dir with Spaces and Caps/My Sub Dir With Spaces and Caps/My Sub Dir File With Spaces and Caps.txt") file.create("./My Dir with Spaces and Caps/My Sub Dir With Spaces and Caps/MySubDirFileJustWithCaps.txt") #new function recursive_replace_lowercase<-function(path=".", replace=" ", with="_", lowercase=TRUE) { # this is the base case filelist<-list.files(path, full.names=TRUE) if (lowercase) { for(filename in filelist) file.rename(filename,gsub(replace,with,tolower(filename))) } else { for(filename in filelist) file.rename(filename,gsub(replace,with,filename)) } # and this is the recursive part dirlist<-list.dirs(path, full.names=TRUE, recursive=FALSE) if(length(dirlist)) { for(dirname in dirlist) recursive_replace_lowercase(dirname, replace=replace, with=with, lowercase=lowercase) } } recursive_replace_lowercase()> Hi maxbre, > Try this: > > recursive_replace<-function(path=".",replace=" ",with="_") { > filelist<-list.files(path,full.names=TRUE) > for(filename in filelist) { > if(length(grep(replace,filename))) > file.rename(filename,gsub(replace,with,filename)) > } > dirlist<-list.dirs(path,full.names=TRUE,recursive=FALSE) > if(length(dirlist)) { > for(dirname in dirlist) > recursive_replace(dirname,replace=replace,with=with) > } > } > > Jim > > > On Fri, Apr 10, 2015 at 5:58 AM, maxbre <mbressan at arpa.veneto.it> wrote: > >> this is my reproducible example >> >> setwd(".") >> 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 >> space.txt") >> >> now I need to rename recursively all dirs and files in order to get rid >> of >> spaces >> >> 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 >> language >> >> any hint much appreciated >> >> thanks >> >> >> >> >> >> >> -- >> 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. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >