Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files with specific word and file extension

Tags:

r

I have a bunch of files in a directory that contain various extensions, but the ones I'm most interested in are *.bil. For each year there are 12 files. What I'm stuck on is matching a year with *.bio, so my list will have 12 files for year 2000. Example filenames:

**** Edit (added actual filenames):

PRISM_tmin_stable_4kmM2_200001_bil.bil
PRISM_tmin_stable_4kmM2_200002_bil.bil

Code:

Filenames <- list.files("/../directory", pattern = "//.bil")

This will select all*.bil files but there are hundreds, so I need to specify only year 2000.

Any ideas?

like image 580
Vedda Avatar asked Sep 10 '25 14:09

Vedda


2 Answers

The list.files command has the options for wildcards, so you should be able to do something like:

list.files("/../directory", pattern = "*_2000*//.bil")

or maybe

list.files("/../directory", pattern = ".*_2000.*\\.bil")

I'm not 100% clear on whether list.files uses a regex pattern and I don't have access to R at the moment, so let me know if that works.

like image 114
lukevp Avatar answered Sep 13 '25 03:09

lukevp


This should also work, to iterate through the PRISM folders, and only pull out the .bil pattern (you need to keep the other files in the same folder so that it understands the raster data the .bil file comes with). recursive=T allows you to pull from multiple folders in your path (or directory), and by setting pattern you will only pull out files with JUST the .bil extension (not asc.bil, etc).

filenames  <- list.files(path="PRISM",
                     recursive=T,
                     pattern="\\.bil$"
                     ,full.names=T)

You can add the above code in with the details above specifying the year 2000.

like image 26
kslayerr Avatar answered Sep 13 '25 02:09

kslayerr