Skip to content
Prev 18612 / 63424 Next

list.files(recursive=T) does not return directory names

Hi,

the R.utils package has a function listDirectory() that returns the 
directory names too.  (I've made some changes to the function recently, 
which is not in the CRAN version, so get it from http://www.braju.com/R/ 
instead.)

The package also has isFile() and isDirectory() to test if a pathname 
refers to an existing file and directory, respectively.  These are not 
"vectorized" (yet), so you have to call them with sapply() if you have 
many pathnames, e.g.

 > path <- file.path(R.home(), "share")
 > ld <- listDirectory(path, recursive=TRUE, fullNames=TRUE)
 > ld[sapply(ld, isDirectory)]
  [1] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses"
  [2] "C:\\PROGRA~1\\R\\rw2011pat/share/locale"
  [3] "C:\\PROGRA~1\\R\\rw2011pat/share/make"
...
[27] "C:\\PROGRA~1\\R\\rw2011pat/share/perl/R"
[28] "C:\\PROGRA~1\\R\\rw2011pat/share/perl/Text"
 > ld[sapply(ld, isFile)]
  [1] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/Artistic"
  [2] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/BSD"
  [3] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/GPL-2"
...
[50] "C:\\PROGRA~1\\R\\rw2011pat/share/texmf/ts1aett.fd"
[51] "C:\\PROGRA~1\\R\\rw2011pat/share/texmf/upquote.sty"

Hope this helps.

BTW, this package also have functions to read Windows Shortcuts files 
(*.lnk) and the function filePath("data", "raw", expandLinks="any") will 
recognize if any part is a shortcut to another directory, e.g. data.lnk 
links to another directory containing subdirectory "raw" (and directory 
data/ does not exist).  (filePath() also not vectorized). 
listDirectory() does not follow Windows Shortcuts.

Henrik
bill at insightful.com wrote: