根据 .txt 文件中存储的列表复制文件

问题描述 投票:0回答:3

我有一个源文件夹 目标文件夹 我想要从源文件夹复制到目标文件夹的文件列表,已保存到 .txt 文件

listtocopy.txt 如下 - 不确定它是否重要,但它们是 Anabat ZC 文件。

S5281925.35#
S5282317.26#
S5290100.39#
S5281859.28#
S5281932.18#
S5290420.20#

我不想复制所有文件。

我是 R 新手 - 这是我到目前为止所拥有的 - 但它不起作用。我认为它没有将该列表识别为文件名“列表”。

# 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)
r list copy
3个回答
1
投票

我认为文本文件的读取方式有问题? 无论如何,这有效。感谢所有的回答。

# 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)
}

0
投票

你可以尝试这样的事情:

setwd(current.folder) 
for(i in list_of_files)
{
file.copy(i, new.folder)
}

file.copy()
不适用于矢量,这就是为什么你必须使用
for
。 如果 list_of_files 仅包含文件名而不包含路径(file.txt、file2.txt...),则可以使用
setwd()


0
投票

答案很晚,但为了完整性而添加。

file.copy()
现在可以使用向量(如果以前没有)。要将列表对象中的所有文件复制到新目录:

# Sample data
list <- read.table(text = "S5281925.35#
S5282317.26#
S5290100.39#
S5281859.28#
S5281932.18#
S5290420.20#", header = FALSE)

# Add full path to file names
list$paths <- paste0("H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/", 
                     list$V1, 
                     ".zc")

list$paths
[1] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5281925.35.zc"
[2] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5282317.26.zc"
[3] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5290100.39.zc"
[4] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5281859.28.zc"
[5] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5281932.18.zc"
[6] "H:/Documents/1_PhD_Network/Auto_ID/Anabat7_11/S5290420.20.zc"

# Copy files to new directory
file.copy(list$paths, "H:/Documents/1_PhD_Network/Auto_ID/Scan_outputs")
© www.soinside.com 2019 - 2024. All rights reserved.