0

I have a set of 16 folders, each of one having sub-folders. I want a code to go into each of those 16 folders, check all the sub-paths and import only those files that fulfill the following conditions: either they contain "B02.jp2" OR "B03.jp2" OR "B04.jp2" OR "B08.jp2"

Here is a screenshot of the files I want to select

Here is the code I am using so far:

This one works perfectly but it only uses one condition (B08.jp2$)

path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern =  "B08.jp2$")

I have been trying to combine multiple patterns but without success: here are my attempts

Based on: list.files pattern argument in R, extended regular expression use

path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern =  glob2rx("B08.jp2$*B03.jp2$"))
path<-character(0)

Based on: R list files with multiple conditions

path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern = "B08.jp2$ | B03.jp2$")
path<-character(0)

-- EDIT --

I have change my data slightly and would like to import them in a different way. My files are now called:

B02_10m.jp2
B03_10m.jp2
B04_10m.jp2
B08_10m.jp2

B05_20m.jp2
B06_20m.jp2
B07_20m.jp2
B8A_20m.jp2
B11_20m.jp2
B12_20m.jp2

They are located in different sub-folders. That's way I am using recursive=TRUE.

I have trying with the following options to combine the conditions but it's not working.

S2 <- "my/path"
S2 <- list.files(S2, recursive = TRUE, full.names = TRUE, pattern =  "B0[2348]_10m.jp2$ | B(0[567]_20m)|(1[12]_20m)|(8A_20m).jp2$")

S2 <- "my/path"
S2 <- list.files(S2, recursive = TRUE, full.names = TRUE, pattern =  "B0[2348]_10m | B(0[567]_20m)|(1[12]_20m)|(8A_20m).jp2$")
GCGM
  • 901
  • 1
  • 17
  • 38

1 Answers1

4

Try this:

list.files(path, recursive = TRUE, full.names = FALSE, 
           pattern = "B0[2348].jp2$")

The pattern accepts a regular expression.

talat
  • 68,970
  • 21
  • 126
  • 157
  • Totally works, much easier than the solutions I was trying! Thanks a lot – GCGM Nov 23 '17 at 12:43
  • Small comment, what if I want to import also the files that end with B11, B12 and B8A. I have tried to use `"B0[2348].jp2$ | B11.jp2$ | B12.jp2$ | B8A.jp2$"` but is not working – GCGM Dec 05 '17 at 09:57
  • Having the following error message: `object X not found`. From `?grep', I see that I have to specify the path where the conditions have to be matched. Is that correct? – GCGM Dec 05 '17 at 11:02
  • 1
    @GCGM ah, sorry, of course you have to put that regex into the list.files function. It should then be `list.files(path, recursive = TRUE, full.names = FALSE, pattern = "B((0[2348])|(1[12])|(8A)).jp2$")` – talat Dec 05 '17 at 11:04
  • Thanks again :) have to learn more about those logical patterns – GCGM Dec 05 '17 at 11:30
  • I have updated my question to ask for a related issue – GCGM Dec 07 '17 at 10:59
  • @GCGM, please ask a new question. The original question has been answered and is closed. – talat Dec 07 '17 at 11:10
  • https://stackoverflow.com/questions/47694848/list-files-with-multiple-conditions-part2 – GCGM Dec 07 '17 at 12:09