parent.folder <- "C:/Desktop/dir"
files <- list.files(path = parent.folder, full.names = T, recursive = T, include.dirs = T)
之后您需要选择相关文件:
files <- files[grep("wang\\.tax\\.sum", files)]
(注意点之前的双转义:\\.
- 点对于 grep 有特殊含义。)
或者你可以这样做pattern
一步完成 list.files 的参数:
files <- list.files(path = parent.folder, full.names = T, recursive = T, include.dirs = T, pattern = "wang\\.tax\\.sum")
创建新目录:
dir.create("taxsum", recursive = T)
现在您需要创建新文件名:
newnames <- paste0("taxsum/", gsub("/|:", "_", files))
# replace "special" characters with underscore
# so that your file names will be different and contain the
# original path
# alternatively, if you know that file names will be different:
newnames <- paste0("taxsum/", basename(files))
现在你可以使用mapply
复制(同样可以用for
需要一点额外的努力):
mapply(file.copy, from=files, to=newnames)