Copy files based on a list stored in .txt file

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 02:44:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!