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

RDirectory

R Problem Overview


This may be a very easy question for someone - 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. Thanks in advance.

R Solutions


Solution 1 - R

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

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

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


I couldn't find a base R function to do this, but it would be pretty easy to write your own using:

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

Update: here's a function (now corrected 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))
}

Solution 2 - R

base R now includes a list.dirs function, so home-brewed variants are no longer necessary.

For example:

list.dirs('.', recursive=FALSE)

Solution 3 - R

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).

Solution 4 - R

list.dirs <- function(...) {
    x <- dir(...)
    x[file_test("-d", x)]
}

might be of use?

How might we do this recursively? (the recursive argument of dir breaks these functions because it never returns directory names, just the files within each directory, etc...).

Solution 5 - R

What about something like this, give it a try:

dir('.')[file.info(dir('.',full.names=T))$isdir]

Solution 6 - R

You mention that you don't want to shell out to a Linux/UNIX command but I assume its ok to shell out to a Windows command. In that case this would do it:

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

and this would do it recursively:

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

Normally I would prefer the platform independent solutions of others here but particularly for interactive use where you are just concerned with getting the answer as simply and directly as possible this may be less work.

Solution 7 - R

I had this problem a while back and used this recursive code to find all directories. Perhaps this can be of use?

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser297400View Question on Stackoverflow
Solution 1 - RJoshua UlrichView Answer on Stackoverflow
Solution 2 - RjbaumsView Answer on Stackoverflow
Solution 3 - RTal GaliliView Answer on Stackoverflow
Solution 4 - RGavin SimpsonView Answer on Stackoverflow
Solution 5 - RFranciscoAView Answer on Stackoverflow
Solution 6 - RG. GrothendieckView Answer on Stackoverflow
Solution 7 - RDavid OView Answer on Stackoverflow