How to get a list of directories in a directory, for example list.files (), but instead "list.dirs ()"

This may be a very simple question for someone - I can use list.files() to get a list of files in a given directory, but if I want to get a list of directories, how can I do this? Is it somehow right in front of me as an option in list.files() ?

Also, I am using Windows, so if the answer is to execute a Linux / unix command, this will not work for me.

.NET, for example, has a Directory.GetFiles() method and a separate Directory.GetDirectories() method, so I decided that R would have a similar pair. Thanks in advance.

+42
directory r
Jan 20 '11 at 16:32
source share
7 answers

Update: A list.dirs function was added to the base package in edition 54353, which was included in the release of R-2.13.0 in April 2011.

 list.dirs(path = ".", full.names = TRUE, recursive = TRUE) 

So, my function below was only useful for a few months. :)




I could not find the basic R function to do this, but it would be pretty easy to write my own use:

 dir()[file.info(dir())$isdir] 

Update: here's the function (now fixed for Timothy Jones comment):

 list.dirs <- function(path=".", pattern=NULL, all.dirs=FALSE, full.names=FALSE, ignore.case=FALSE) { # use full.names=TRUE to pass to file.info all <- list.files(path, pattern, all.dirs, full.names=TRUE, recursive=FALSE, ignore.case) dirs <- all[file.info(all)$isdir] # determine whether to return full names or just dir names if(isTRUE(full.names)) return(dirs) else return(basename(dirs)) } 
+57
Jan 20 '11 at 16:41
source share

base R now includes a list.dirs function , so home shaft options are no longer needed.

For example:

 list.dirs('.', recursive=FALSE) 
+17
Dec 08
source share

Just update this thread:

I see that in the new version of R (I am currently using 2.5.1) now there is a list.dirs function included in the basic installation:

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

+16
Aug 28 '12 at 11:37
source share
 list.dirs <- function(...) { x <- dir(...) x[file_test("-d", x)] } 

might be useful?

How can we do this recursively? (the recursive dir argument destroys these functions because it never returns directory names, only files in each directory, etc.).

+8
Jan 20 '11 at 16:52
source share

You mentioned that you do not want to distribute the Linux / UNIX command, but I assume that its shell will be removed to the Windows command. In this case, it would do this:

 shell("dir/ad/b", intern = TRUE) 

and this will be recursive:

 shell("dir/ad/b/s", intern = TRUE) 

Usually I would prefer platform-independent solutions of others, but especially for interactive use, when you are just interested in getting an answer as simple and possible as possible, it can be less.

+2
Jan 20 2018-11-21T00:
source share

Something like this, try:

 dir('.')[file.info(dir('.',full.names=T))$isdir] 
+2
Apr 29 '14 at 3:31 on
source share

I had this problem a while ago, and I used this recursive code to search all directories. Perhaps this could be helpful?

 list.dirs <- function(parent=".") # recursively find directories { if (length(parent)>1) # work on first and then rest return(c(list.dirs(parent[1]), list.dirs(parent[-1]))) else { # length(parent) == 1 if (!is.dir(parent)) return(NULL) # not a directory, don't return anything child <- list.files(parent, full=TRUE) if (!any(is.dir(child))) return(parent) # no directories below, return parent else return(list.dirs(child)) # recurse } } is.dir <- function(x) # helper function { ret <- file.info(x)$isdir ret[is.na(ret)] <- FALSE ret } 
-one
Apr 07 '14 at 23:11
source share



All Articles