Home > Software engineering >  Listing *.tif files in directories meeting certain criteria
Listing *.tif files in directories meeting certain criteria

Time:10-08

I want to list all *.tif files in directories that contain specific numeric values. Example: Path_source <- "C:/Files/" Within this folder, I have 1000 folders, each named jobXXX where XXX stands for numeric value 1-999. So there is a folder job0, job1, ... , job999. Within each jobXXX folder I have other folders and files. From this structure of folders I want to extract all files with the following pattern:

Year <- 2021

file.ls <- list.files(path=Path_source, pattern=paste0("Text_",Year,".*\\.tif"),all.files = TRUE, full.names = TRUE, recursive = TRUE)

So my missing element is now to filter the directories for folders with specific job numbers:

jobs <- c(1,14,45,67,129,834)

Example outcome:

> file.ls

[1] "C:/Files/job14/folder12/Text_2021_anothertext_abc.tif"

[2] "C:/Files/job45/folder81/Text_2021_anothertext_efg.tif"

Which function should I use?

Thank you

CodePudding user response:

The following function creates part of the filenames. Then a sapply loop gets the directories in the vector returned by the function.

make_dir_name <- function(Path_prefix, job_prefix, job_number){
  fmt_string <- paste0(Path_prefix, job_prefix, "%d[^[:digit:]] ")
  sprintf(fmt_string, job_number)
}

jobs_dirs <- make_dir_name(Path_source, "job", jobs)
i <- sapply(jobs_dirs, \(d) grep(d, file.ls))
i <- unname(unlist(i))
file.ls[i]

Test data

file.ls <-
c("C:/Files/job14/folder12/Text_2021_anothertext_abc.tif", 
  "C:/Files/job45/folder81/Text_2021_anothertext_efg.tif")

CodePudding user response:

Sticking with base r this might be of help.

files <- paste("job", seq(1,10), ".tif", sep = "")

numbers <- seq(1,10, by = 2)
files_I_Want <- paste("job",numbers, ".tif",sep = "")

files[files %in% files_I_Want]

Using the %in% operator I can see what files are in my constructed files I want vector.

  • Related