Vedda
Vedda

Reputation: 7445

List files with specific word and file extension

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?

Upvotes: 4

Views: 10651

Answers (2)

kslayerr
kslayerr

Reputation: 819

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.

Upvotes: 2

lukevp
lukevp

Reputation: 715

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.

Upvotes: 5

Related Questions