How can I copy files from folders and subfolders to another folder in R?
Asked Answered
B

1

7

I would like to copy files only from 1 root folder that has 100's of folders and subfolders. I do not want to copy the folders. I just want to copy all the files (*.iso, *.txt, *.docx, *.pdf etc.) there are in these folders to another folder.

My code:

setwd("/Users/RLearner/Desktop/RDMS")

if (file.exists(list.files(path=".",recursive=TRUE)))
  file.copy(from=".", to="/Users/RLearner/Desktop/Test", recursive=TRUE)

But this code is copying the root folder as it is into my desired Test folder. I just want to copy the files that these folders have?

Brandeebranden answered 18/10, 2014 at 11:28 Comment(2)
Does ?file.rename help?Spite
No, I thought it was for renaming files and folders?Brandeebranden
I
8

I would do:

from.dir <- "/Users/RLearner/Desktop/RDMS"
to.dir   <- "/Users/RLearner/Desktop/Test"
files    <- list.files(path = from.dir, full.names = TRUE, recursive = TRUE)
for (f in files) file.copy(from = f, to = to.dir)
Intradermal answered 18/10, 2014 at 11:36 Comment(2)
It didn't work for me:( Can you please explain this? Thanks EDIT: IT WORKED!!!! The error was from my side. What a genius you are!! Thank you. Have a great weekend. @IntradermalBrandeebranden
To copy files in nested folders I used file.copy(list.files(from.dir, full.names = TRUE), to.dir, recursive=TRUE).Terrorize

© 2022 - 2024 — McMap. All rights reserved.