How to capture files with the same name only with the .pdf extension

51 Views Asked by At

enter image description here

I'm using R, because I need to capture files with the same name only with the .pdf extension See the attached image. The file with the extension in excel doesn't interest me. The files have similar names

I tried according to the code below but it returns the files with the excel extension too

pdfs = list.files(pattern="COC04", recursive = TRUE, full.names = TRUE)

I need to filter by COC04 because there are other pdf files that I'm not interested in

1

There are 1 best solutions below

0
Joan On BEST ANSWER

maybe you could try this regex:

(.)*(COC04){1}(.)*(.pdf)$

The breakdown:

  • (.)* = any character for an unlimited amount of times (also zero)
  • (.)*(COC04) = After the previous, it has to be followed by COC04 (1 time)
  • (.)*(COC04){1}(.)* = After finding the COC04, it can be followed by any other string.
  • (.)*(COC04){1}(.)*(.pdf)$ = the string should end with .pdf

So: list.files(pattern = "(.)*(COC04){1}(.)*(.pdf)$" full.names = TRUE, recursive = TRUE)

I hope this helps!