Alex Rivera | Logout

How to obtain a list of directories within a directory, like list.files(), but instead "list.dirs()"

Asked 2011-01-20T16:32:21.453
102

I am able to use list.files() to obtain a list of files in a given directory, but if I want to get a list of directories, how would I do this? Is it somehow right in front of me as an option within list.files()?

Also, I'm using Windows, so if the answer is to shell out to some Linux/unix command, that won't work for me.

.NET for example has a Directory.GetFiles() method, and a separate Directory.GetDirectories() method, so I figured R would have an analogous pair.

Edit
Report

1 Answer

19

Just to update this thread:

I see that in the newer version of R (currently I'm using 2.5.1), there is now a list.dirs function included in the base install:

list.dirs implicitly has all.files = TRUE, and if recursive = TRUE, the answer includes path itself (provided it is a readable directory).

answered 2012-08-28T11:37:40.447

Your Answer