# Finding specific terms in several files.

#### winecoding

##### New Member
I have several documents, and would like to see which files contain a given term. How to do that in R?

#### Dason

Can you give an example of what you mean? I don't really get what you're trying to do I guess.

#### winecoding

##### New Member
The scenario is as follows: assume we have a folder C:\Desktop\test. There have 8 files, data1.txt, .... data8.txt, under this folder. Some of these files may contain a specific term, e.g., colorcoding. I would like to know how many files contain this specific term, and which are them. This can be done under Unix/Linux using shell command. But I need to know how to do that within R program. Thanks.

#### Dason

Code:
filegrep <- function(filename, pattern){
}

path <- "C:/Desktop/test"
pat <- "colorcoding"
out <- sapply(dir(path), filegrep, pattern = pat)
which(out)

#### winecoding

##### New Member
Dason, thanks for the code. I tried, and it gives me the following error message

Error in file(con, "r") : cannot open the connection
In file(con, "r") : cannot open file 'data1.txt': No such file or directory
However, the data1.txt is contained in the folder

Code:
filegrep <- function(filename, pattern){
}

path <- "C:/Desktop/test"
pat <- "colorcoding"
out <- sapply(dir(path), filegrep, pattern = pat)
which(out)

#### Dason

filegrep <- function(filename, pattern){
which(out)