mercredi 30 mai 2018

How to copy files in subfolders to another subfolder based on pattern in R

I am trying to copy files from one directory to another one, based on some patterns. In particular, I have a parent folder (names PF) that contains 10 subfolders. Each of these subfolders contains 20 subfolders. Each of these subfolders contains hundreds of files that have the particularity to have different names (a, b, c, d, e, f for example). In a second parent folder (SPF) I have different subfolders named like the files (a, b, c, d, e, f). I would like to copy all the files named (a, b, c, d, e, f) to the new subfolder (a,b,c,d,e,f) and that their names match. So for examples, all the files named a will go to the subfolder a.

I tried a code that does not work:

PF <- "/PATH/PF"
SPF<- "/PATH/SPF"
new.dir<- list.dirs(SPF, recursive=TRUE)

names<- c("a", "b", "c", "d", "e")

for (i in 1: length (names)){

e<-list.files("PATH/PF", recursive = TRUE,pattern=names[i])
file_new<- new.dir[grep(names[i], new.dir)]

file.copy(e[i], file_new[i])

}

I do not have an error message but none of my files are copied so the code doesn't work and I cannot see how to fix it.

Thanks for any help!

Aucun commentaire:

Enregistrer un commentaire