从 csv 复制粘贴文件列表
Copy paste list of files from csv
我有一个位于多个子目录中的 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)
您不需要自定义函数。
# 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)
我有一个位于多个子目录中的 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)
您不需要自定义函数。
# 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)