Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all sub directories that only contain files

Tags:

c#

.net

I have a path and I want to list the subdirectories under it, where each subdirectory doesn't contain any other directory. (Only those subdirectories which don't contain folders, but only files.)

Any smart way to do so?

like image 590
Night Walker Avatar asked Mar 09 '10 09:03

Night Walker


2 Answers

It is my understanding that you want to list the subdirectories below a given path that contain only files.


static IEnumerable<string> GetSubdirectoriesContainingOnlyFiles(string path)
{
  return from subdirectory in Directory.GetDirectories(path, "*", SearchOption.AllDirectories)
         where Directory.GetDirectories(subdirectory).Length == 0
        select subdirectory;
}
like image 118
Håvard S Avatar answered Sep 21 '22 06:09

Håvard S


DirectoryInfo

DirectoryInfo dInfo = new DirectoryInfo(<path to dir>);
DirectoryInfo[] subdirs = dInfo.GetDirectories();
like image 41
RvdK Avatar answered Sep 21 '22 06:09

RvdK