roschu
roschu

Reputation: 776

list files in folder matching an exact filename

I have a number of files in my folder, e.g.:

I only wish R to find "file1.txt". But if I use

list.files(pattern = "file1.txt")

R will also return the other two files from my example. Any ideas how I could solve this?

Thanks!

Upvotes: 6

Views: 4559

Answers (1)

Henrik
Henrik

Reputation: 14460

Use regular expressions (see ?regex):

list.files(pattern = "^file1\\.txt$")

^ is the regular expression denoting the beginning of the string,
\\ escapes the . to make it a literal .,
$ is the regular expression denoting the end of the string.

In sum, this is the regular expression capturing exactly file1.txt and nothing else.

Upvotes: 14

Related Questions