问题
I have a source folder Destination folder List of files I want to be copied from source folder to destination folder, which has been saved to a .txt file
listtocopy.txt is as below - not sure if it's important but they are Anabat ZC files.
S5281925.35#
S5282317.26#
S5290100.39#
S5281859.28#
S5281932.18#
S5290420.20#
I do not want all files to be copied.
I'm new to R -this is what I have so far - but it's not working. I think it's not recognizing the list as a 'list' of file names.
# Copy based on list
# identify the folders
current.folder <- "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11"
new.folder <- "H:/Documents/1_PhD_Network/Auto_ID/Scan_outputs"
#read listtocopy and assign to list
list<-read.delim("H:/Documents/1_PhD_Network/Auto_ID/Scan_outputs/listtocopy.txt")
# copy the files to the new folder
file.copy(list, new.folder)
回答1:
I think there was a problem with how the text file was being read?? anyway, this works. thanks to all that answered.
# identify the folders
current.folder <- "C:/Users/Amanda/Desktop/testcopy/Anabat7_11"
new.folder <- "C:/Users/Amanda/Desktop/testcopy/Scan_outputs"
# find the files that you want
list_of_files <- read.delim("listtocopy.txt",header = F)
#check
print(list_of_files)
#copy vector
setwd(current.folder)
for(i in list_of_files)
{
file.copy(i, new.folder)
}
回答2:
You can try something like this:
setwd(current.folder)
for(i in list_of_files)
{
file.copy(i, new.folder)
}
file.copy() doest work with vectors, thats why you have to use for.
You can use setwd() if list_of_files contains just file names and not paths (file.txt, file2.txt,...)
来源:https://stackoverflow.com/questions/51906622/copy-files-based-on-a-list-stored-in-txt-file