【问题标题】:Copy paste list of files from csv从 csv 复制粘贴文件列表
【发布时间】:2018-11-16 19:10:23
【问题描述】:

我有一个 txt 文件的 csv 列表(没有完整路径,如下所示:filea.txt),它们位于多个子目录中。我想将所有这些文件复制粘贴到一个目录中。

为了让事情变得更加困难,这个 txt 文件的 csv 列表没有可重复的模式。此列表中的名称必须与目录中所有 txt 文件的列表相匹配。

有人知道怎么做吗?

这是我的尝试:

 # Target and source
 source <- "C:/Users/blue/Desktop/A"
 target <- "C:/Users/blue/Desktop/B"

 # List of all txt files in main directory
 all.files  <- list.files(path = source,
                         recursive = TRUE,
                         pattern = ".txt",
                         full.names = TRUE)

 # List of specific txt files to extract
 extract.files <- read.csv(paste0(source, "/extract.csv"), head = FALSE, sep=",")

 # Somehow match list of specific files with list of all txt files here

 # Function to copy paste
 my.file.rename <- function(from, to) {
  todir <- dirname(to)
  if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
  file.copy(from = from,  to = to)
 } 

 # Copy paste
 my.file.rename(from = source,to = target)

【问题讨论】:

    标签: r copy-paste


    【解决方案1】:

    您不需要自定义函数。

    # Target and source
    source <- "C:/Users/blue/Desktop/A"
    target <- "C:/Users/blue/Desktop/B"
    
    # List of all txt files in main directory
    all.files  <- list.files(path = source,
                             recursive = TRUE,
                             pattern = ".txt",
                             full.names = TRUE)
    
    # List of specific txt files to extract
    extract.files <- read.csv(paste0(source, "/extract.csv"), head = FALSE, sep=",")
    
    toCopy <- all.files[which(basename(all.files) %in% unlist(extract.files))]
    
    file.copy(toCopy, target)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2020-02-16
      相关资源
      最近更新 更多