1

我有两个文件夹目录

directory1<-"C:/Folder1/"
directory2<-"C:/Folder2/"

文件夹 1 包含文件

"123456.pdf", "234567.pdf", "345678.pdf", "456789.pdf"

文件夹 2 包含文件

"123456_Jon.pdf","234567_Mike.pdf", "345678_Bill.pdf","456789_Ralph.pdf","Random_file.pdf"

如果文件夹 1 和 2 中的 pdf 共享前 6 个数字,那么我想加入它们并创建一个directory1名为的新文件

"123456_Join.pdf","234567_Join.pdf","345678_Join.pdf","456789_Join.pdf"
4

1 回答 1

2

假设您的文件名存储在

files_1 <- c("123456.pdf", "234567.pdf", "345678.pdf", "456789.pdf")
files_2 <- c("123456_Jon.pdf","234567_Mike.pdf", "345678_Bill.pdf","456789_Ralph.pdf","Random_file.pdf")

library(qpdf)

for (file in files_1) {
  ext_num <- sub("(^\\d{6}).*", "\\1", file)
  target  <- grepl(paste0("^", ext_num), files_2)

  if (!any(target)) next
  
  pdf_combine(c(file, file.path(directory2, files_2[target])),
              output = paste(directory1, ext_num, "Join.pdf", sep = "_"))
  
}

应该给你你想要的输出。

于 2021-08-20T22:43:04.463 回答