我有两套来自不同文件夹的 pdf,我根据相同的名称将它们作为一个加入,并输出到第一个 pdf 组的同一文件夹中

I have two sets of pdf from different folders that i went to join as one based on the same name and output in the same folder of first pdf group

我有两个文件夹目录

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"

假设您的文件名存储在

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 = "_"))
  
}

应该会给你想要的输出。