tzi
tzi

Reputation: 67

List files starting with a specific character

I have a folder which contains files of the following names "5_45name.Rdata"and "15_45name.Rdata".

I would like to list only the ones starting with "5", in the example above this means that I would like to exclude "15_45name.Rdata".

Using list.files(pattern="5_*name.Rdata"), however, will list both of these files.

Is there a way to tell list.files() that I want the filename to start with a specific character?

Upvotes: 6

Views: 5522

Answers (1)

akrun
akrun

Reputation: 887901

We need to use the metacharacter (^) to specify the start of the string followed by the number 5. So, it can a more specific pattern like below

list.files(pattern ="^5[0-9]*_[0-9]*name.Rdata")

Or concise if we are not concerned about the _ and other numbers following it.

list.files(pattern = "^5.*name.Rdata")

Upvotes: 7

Related Questions