title: How to use R to iterate through sub-folders and read text files inside?
body: <p>I'm wondering why this doesn't work so I decided to post this problem here.
  I'm doing text mining now, and what I'm supposed to do is to iterate through subfolders and read all the text files in each of these subfolders.</p>
  
  <p>so, basically the folders are structured in this way:</p>
  
  <pre><code>==============================================
  branch1    branch 2     txt.file
  1 -------- 2002----------a
    ---------2003----------b
                 ----------c
  2 ---------2004----------d
    ---------2005----------e
                 ----------f
  .
  .
  ==============================================
  </code></pre>
  
  <p>the code I've written is sth like below:</p>
  
  <pre><code>=============================================
  setwd("C:/Users/JY/Desktop/research/DATA/test")
  parent.folder&lt;-"C:/Users/JY/Desktop/research/DATA/test"
  sub.folders1 &lt;- list.dirs(parent.folder, recursive=TRUE)[-1]
  sub.folders2 &lt;- list.dirs(sub.folders1, recursive=FALSE)
  r.scripts &lt;- file.path(sub.folders2)
  =============================================
  </code></pre>
  
  <p>the problem is that R cannot read all the txt files in each sub-directory
  and I don't understand why this happens. </p>
  
  <pre><code>=============================================
  for (j in r.scripts){
    files.v &lt;- dir(r.scripts,"\\.txt$")
  }
  =============================================
  </code></pre>
  
  <p>if I run this code, it doesn't return anything.
  So I tried different commands such as for(j in 1:length(r.scripts)) etc,
  but it partially returns txt files from the first/last folder.</p>