我有一个位于多个子目录中的txt文件的csv列表(没有完整路径,看起来像:filea.txt)。我想将所有这些文件复制粘贴到一个目录中。
更麻烦的是,此CSV文件的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)
答案 0 :(得分:0)
您不需要自定义功能。
# 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)