title: Listing all files matching a full-path pattern in R
body: <p>I am trying to obtain the list of files matching a <strong>full-path</strong> pattern. So far, I have used list.files() but it did not work.</p>
  
  <p>Let's assume that we have the following directory organization:</p>
  
  <pre><code>results
     |- A
     |  |- data-1.csv
     |  |- data-2.csv
     |
     |- B
        |- data-1.csv
        |- data-2.csv
  </code></pre>
  
  <p>Then the following command:</p>
  
  <pre><code>list.files(pattern='data-.*\\.csv', recursive=TRUE)
  </code></pre>
  
  <p>will return all the files matching the pattern. This works, but the problem appears when using a <strong>full-path</strong> pattern. For instance, if I want to obtain all the CSV files from directory <em>results/A</em>, I could do:</p>
  
  <pre><code>list.files(pattern='results/A/data-.*\\.csv', recursive=TRUE)
  </code></pre>
  
  <p>This does not work, though. Somehow, it seems like R is not able to use a full-path pattern as a regular expression. In this case, the solution could be to just use <em>results/A</em> as the base path. But in more complex problems, that cannot be done. For instance, at some point we may want to match the subdirectories containing only characters:</p>
  
  <pre><code>list.files(pattern='results/[A-Z]+/data-.*\\.csv', recursive=TRUE)
  </code></pre>
  
  <p>Is it possible to do this in R?</p>
  
  <p><strong>UPDATE:</strong> After using ad hoc solutions for a while, I decided to stop typing the same again and again. So, I created a <a href="https://github.com/betabandido/exptools">library</a> for simplifying this task.</p>