C# - Exclude directories and files from Directory.GetDirectories() and Directory.GetFiles()

2.8k Views Asked by At

I need to count files and directories inside a specified directory path. But I want to exclude .git folder and files inside this folder from being counted. I tried -

int maxCount = Directory.GetFiles(loadedDirectoryPath, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(loadedDirectoryPath, "*.*", SearchOption.AllDirectories).Length;

And for excluding .git, I can write -

Directory.GetDirectories(loadedDirectoryPath, "*.*", SearchOption.AllDirectories).Where(item => !item.EndsWith(".git")).ToArray().Length;

This will exclude .git directory, But how can I prevent files (present inside .git directory) being counted?

As per my knowledge, GetDirectories() deals with only folders not with files and GetFiles() deals only with files without caring what directories excluded by GetDirectories().

4

There are 4 best solutions below

0
Ive On BEST ANSWER

what about this:

    var all = Directory.GetFileSystemEntries(path, "*.*",SearchOption.AllDirectories);
    var allCount = all.Count();
    var noGit = all.Where(p =>  !p.Contains(@"\.git\") && !p.EndsWith(@"\.git")).ToArray();
    var noGitCount = noGit.Count();
0
Anu Viswan On

You could do the following. Comments included with code

// List of Folders to be ignore, .git ones
var foldersToIgnore = Directory.GetDirectories(path,".git",SearchOption.AllDirectories);
// list of files, excludes ones in .git folder
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
             .Where(x=>!foldersToIgnore.Any(c=>x.StartsWith(c)));
var directories = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories)
             .Where(x=>!foldersToIgnore.Any(c=>x.StartsWith(c)));
// Count
var maxCount = files.Count() + directories.Count();
0
claudiom248 On

you could try this solution:

var pathToIgnore = loadedDirectoryPath + ".git";
int count =
    Directory.GetFiles(loadedDirectoryPath, "*.*", SearchOption.AllDirectories).Where(d => !d.StartsWith(pathToIgnore))
    .Concat(Directory.GetDirectories(loadedDirectoryPath, "*.*", SearchOption.AllDirectories).Where(d => !d.StartsWith(pathToIgnore)))
    .ToArray()
    .Count();

Note that this solution will only let you specify one folder to exclude, but it's trivial to adapt it to works with multiple folders to exclude.

Let me know if it will be useful.

0
marcofo88 On

As MSDN states here:

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

Therefore, you should be better off by doing something like the following:

var directory = new DirectoryInfo(directoryPath);
var filesCount = directory.EnumerateFiles("*.*", SearchOption.AllDirectories).Count(file => !file.DirectoryName.Contains(".git"));