[R] find directories

mel mel at altk.com
Wed Apr 23 11:25:19 CEST 2008

Prof Brian Ripley a écrit :
> On Wed, 23 Apr 2008, mel wrote:
>> Hello,
>> Function dir() is very useful for finding files.
>> However, is there a analog function for finding directories
>> based eg on a pattern ?
> No.
>> Apologies if I've missed something obvious.
> list.dirs <- function(...)
> {
>     x <- dir(...)
>     x[file_test("-d", x)]
> }

Thanks for the fast answer Prof Ripley.

In fact, I was using (on a tree of directories of depth 4)
x <- dir(..., recursive=TRUE)
and didn't notice that the results from
dir(...) is not included in dir(..., recursive=TRUE)

If I'm not wrong, x <- dir(..., recursive=TRUE)
seems to return the findable *files only* in the tree.
While x <- dir(...)
returns the *files and the directories* present at the top level
of the tree.

The directories I'm searching are unfortunately not at the top level,
But, in my special case, I'm lucky enough that a simple trick allows
to identify the directories.


More information about the R-help mailing list