Marycee
Marycee

Reputation: 149

Move files with list to new directory keeping original sub-directory with R

I have a list of 500,000 files and their paths which I need to move to a new directory but keeping the folder structure.

    > head(list.of.files)
    [1] "F:\\Client X/Geochem/all.txt"                                                                                
    [2] "F:\\Client X/Geochem/Rock Sample.xlsx"                             
    [3] "F:\\Client X/Geochem/DataDump/2006 Humus Sampling/every5.txt"
    > 

I have tried using file.copy to do this but all the files copy over to a single destination folder without keeping the sub-directory structure. The reason that I am doing this with a list is that I am only moving certain files from the original folders. I have gone through all the posts on this but cant seem to find a question that fits this particular problem. My final folder structure should look like this:

    [1] "F:\\Client X Copy/Client X/Geochem/all.txt"
    [2] "F:\\Client X Copy/Client X/Geochem/Rock Sample.xlsx" 
    [3] "F:\\Client X Copy/Client X/Geochem/DataDump/2006 Humus Sampling/every5.txt"  

Here is the code that I used:

    current.folder <- "F:\\Client X"
    new.folder <- "F:\\Client X Copy"
    list.of.files <- list.files(current.folder,full.names=TRUE, recursive = TRUE)
    file.copy(list.of.files, new.folder)

Any suggestions would be hugely appreciated!

Upvotes: 1

Views: 773

Answers (1)

Silence Dogood
Silence Dogood

Reputation: 3597

file.copy does not replicate directory tree structure which can be solved by using xcopy DOS command on Windows platform to create empty directory structure. I have tested this on Windows for tiny test cases and works fine. (rysnc in *nix platforms will serve the same purpose, not tested)

Create File Paths:

list.of.files = c(
"F:\\Client X/Geochem/all.txt"                                                                                
,"F:\\Client X/Geochem/Rock Sample.xlsx"                             
,"F:\\Client X/Geochem/DataDump/2006 Humus Sampling/every5.txt")

list.of.files
#[1] "F:\\Client X/Geochem/all.txt"                                
#[2] "F:\\Client X/Geochem/Rock Sample.xlsx"                       
#[3] "F:\\Client X/Geochem/DataDump/2006 Humus Sampling/every5.txt"

#Use gsub to edit destination paths

list.of.dest.files = gsub("Client X","Client X Copy",list.of.files)
list.of.dest.files
#[1] "F:\\Client X Copy/Geochem/all.txt"                                
#[2] "F:\\Client X Copy/Geochem/Rock Sample.xlsx"                       
#[3] "F:\\Client X Copy/Geochem/DataDump/2006 Humus Sampling/every5.txt"

Replicate Tree Strcuture:

#Create directory tree structure without copying any files
#https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
#Note the usage of single and double quotes to address issue of white space
#in file paths

system('xcopy "F:\\Client X" "F:\\Client X Copy"  /t')

#unix equivalent
#http://serverfault.com/questions/204303/how-do-i-copy-a-directory-tree-but-not-the-files-in-linux
#system("rsync -av -f"+ */" -f"- *" /path/to/src /path/to/dest/")

Copy Files:

for(i in 1:length(list.of.files)){
 cat("Copying file:",list.of.files[i],"\n")
 file.copy(list.of.files[i],list.of.dest.files[i])
}

Would highly recommend babun on Windows,it has an excellent emulation of unix environment with useful utilities such as cp and mv (file copy/move) ,find and grep (search files/dir),diff (differences in files),du (disk usage), etc.

Upvotes: 1

Related Questions