How to list only specific files in R?

I have thousands of different files (with the same extension .img

) in a folder named data

. If I use this:

 dir<- list.files ("C:\\Users\\data", "*.img", full.names = TRUE)


It will list all the files I have in the folder data


I just need to specify files named like this:

 File_yyyymmdd_data.img     (in which yyyymmdd varies for 10 years)


Any idea or hint is greatly appreciated!


source to share

1 answer

try it = list.files(directory.path, recursive = TRUE, pattern=".img")

Where "directory.path" is the path to the directory containing the files you need to read


to be more suitable = list.files(directory.path, recursive = TRUE, pattern="File_[0-9]{8}_data[.]img$")




All Articles