0

I have some files in the directory and their extensions all same. I want list all files but later on would like to ignore some of the file names that contain certain strings. I tried grepl using this answer using-r-to-list-all-files-with-a-specified-extension.

For instance in this example I would like exclude the files which contains 'B' in it. So tried,

file_names <- c('AA','BA','KK','CB')
files <- paste0(file_names,'.txt')

Filter_files <- files[-grepl('.*B.txt|.B*.txt', files)]

Filter_files

 "BA.txt" "KK.txt" "CB.txt"

Interestingly only AA.txt excluded!

Alexander
  • 4,527
  • 5
  • 51
  • 98

1 Answers1

2

This will work:

file_names <- c('AA','BA','KK','CB')
files <- paste0(file_names,'.txt')

Filter_files <- files[!grepl('.*B.*\\.txt', files)]

Filter_files

## "AA.txt" "KK.txt"

These are the changes I made:

  • Instead of -, the grepl is preceded by !, which negates the result of the grepl (i.e., the TRUE results become FALSE and viceversa).
  • To capture all the Bs regardless of where they are located, I search for any character (that's what . indicates) appearing 0 or more times (indicated by the * sign). This way whether the B is at the beginning or the end of the filename it is equally captured.
  • Since . has the special meaning of 'any character' in regex, to have a literal dot in the expression you have to escape it, thus \\. before the txt extension.
Oriol Mirosa
  • 2,756
  • 1
  • 13
  • 15
  • Thanks for the answer. could you little bit explain what the `'` and `\\\` and `*` means. I was using `*` only but really don't know what they are standing for ? In addition If I wanted to add another file names for exclude how can I implement them. Could you add these points to your solution too! Thanks. – Alexander Aug 05 '17 at 17:35
  • 1
    I updated the post with my changes. Let me know if you have any further questions. – Oriol Mirosa Aug 05 '17 at 18:03