Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list files in folder matching an exact filename

Tags:

r

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

  • file1.txt
  • file1.txt_sub
  • file1.txt_sub2

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!

like image 396
roschu Avatar asked Jun 25 '13 10:06

roschu


1 Answers

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.

like image 132
Henrik Avatar answered Oct 05 '22 23:10

Henrik