【问题标题】:Copy files based on a list stored in .txt file根据存储在 .txt 文件中的列表复制文件
【发布时间】:2018-08-20 12:01:33
【问题描述】:

我有一个源文件夹 目标文件夹 我要从源文件夹复制到目标文件夹的文件列表,已保存到 .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)

【问题讨论】:

  • txt文件中的文件名是怎么存储的?你能告诉我们listtocopy.txt的前几行吗?
  • 我想昨天有人问过类似的问题:stackoverflow.com/questions/51893525/…
  • 抱歉它现在在上面
  • @milan 我试图使用该帖子中的信息但没有成功。

标签: r list copy


【解决方案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)
}

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    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()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多