1

I have two folder directory

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

Folder 1 contains file

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

Folder 2 contains file

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

If the pdf's in folder 1 and 2 share the first 6 numbers then i want to join them and create a new file in directory1 named

"123456_Join.pdf","234567_Join.pdf","345678_Join.pdf","456789_Join.pdf"
user35131
  • 1,105
  • 6
  • 18

1 Answers1

2

Suppose your filenames are stored in

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

should give you your desired output.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • I got a message that said `Error in normalizePath(path.expand(path),wislash,mustwork): path[1]- "123456 text_value.pdf": The system cannot find the file specificed` – user35131 Aug 24 '21 at 15:21
  • I'm not sure what causes this error. I added a handling, if the file from `files_1` doesn't have a match in `files_2`. Perhaps this solves the problem. – Martin Gal Aug 24 '21 at 15:36
  • I still get the same error. The first 6 characters of each pdf match though they are formatted differently. Is it cause of the underscore? I forgot to add there is a blank between the first 6 number characters and the name. Though i don't think that would make a difference. – user35131 Aug 24 '21 at 15:44
  • 1
    Is directory 2 not referenced? – user35131 Aug 24 '21 at 16:01
  • I got False for target but i'll try to add files_ <- list.files(path = directory, pattern = "^.pdf" – user35131 Aug 24 '21 at 16:06
  • `Error: '\.' is an unrecognized escape in character string starting ""\."` This worked `list.files(path = directory, pattern = "\\pdf$")` – user35131 Aug 24 '21 at 16:12
  • I get this error message now with `Error: unexpected ')' in " output = paste(directory1, ext_num, "Join.pdf", sep = ""))" > } Error: unexpected '}' in "}"` – user35131 Aug 24 '21 at 16:19
  • It's nearly impossible to debug your code without seeing it. – Martin Gal Aug 24 '21 at 16:30
  • Perhaps the `pdf_combine()` syntax is flawed. I can't check it nomw. – Martin Gal Aug 24 '21 at 16:50
  • 1
    It worked. The issue was the brackets at the end `pdf_combine(input= c(file, file.path(directory2, files_2[target])), output = paste(directory, ext_num, "Join.pdf", sep = "_"))`. You had three after `files_2[target] it should be two. Thank you so much and my apologies. – user35131 Aug 24 '21 at 16:53
  • My apologies for this stupid error. I couldn't test the code since I'm mobile atm. Didn't think of a bracket error... – Martin Gal Aug 24 '21 at 17:08