Find number of files with a specific extension, in all subdirectories
39583 просмотра
7 ответа
Is there a way to find the number of files of a specific type without having to loop through all results inn a Directory.GetFiles() or similar method? I am looking for something like this:
int ComponentCount = MagicFindFileCount(@"c:\windows\system32", "*.dll");
I know that I can make a recursive function to call Directory.GetFiles , but it would be much cleaner if I could do this without all the iterating.
EDIT: If it is not possible to do this without recursing and iterating yourself, what would be the best way to do it?
Автор: Espo Источник Размещён: 22.10.2019 01:24Ответы (7)
35 плюса
You should use the Directory.GetFiles(path, searchPattern, SearchOption) overload of Directory.GetFiles().
Path specifies the path, searchPattern specifies your wildcards (e.g., *, *.format) and SearchOption provides the option to include subdirectories.
Свойство Length возвращаемого массива этого поиска обеспечит правильное количество файлов для вашего конкретного шаблона поиска и опцию:
string[] files = directory.GetFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories);
return files.Length;
РЕДАКТИРОВАТЬ: В качестве альтернативы вы можете использовать метод Directory.EnumerateFiles
return Directory.EnumerateFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories).Count();
Автор: Jon Limjap
Размещён: 26.08.2008 08:53
8 плюса
Самый лучший способ использовать linq:
var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
select file).Count();
Автор: Dean
Размещён: 15.09.2011 12:40
7 плюса
Вы можете использовать эту перегрузку GetFiles:
и этот член SearchOption:
AllDirectories - Включает текущий каталог и все подкаталоги в операции поиска. Этот параметр включает точки повторного анализа, такие как подключенные диски и символические ссылки в поиске.
GetFiles возвращает массив строк, поэтому вы можете просто получить длину, которая является количеством найденных файлов.
Автор: jfs Размещён: 26.08.2008 08:536 плюса
Я искал более оптимизированную версию. Так как я не нашел это, я решил закодировать это и разделить это здесь:
public static int GetFileCount(string path, string searchPattern, SearchOption searchOption)
{
var fileCount = 0;
var fileIter = Directory.EnumerateFiles(path, searchPattern, searchOption);
foreach (var file in fileIter)
fileCount++;
return fileCount;
}
Все решения, использующие GetFiles / GetDirectories, являются довольно медленными, поскольку все эти объекты должны быть созданы. Используя перечисление, он не создает никаких временных объектов (FileInfo / DirectoryInfo).
см. примечания http://msdn.microsoft.com/en-us/library/dd383571.aspx для получения дополнительной информации
Автор: Sauleil Размещён: 23.03.2011 06:581 плюс
Используя рекурсию, ваш MagicFindFileCount будет выглядеть так:
private int MagicFindFileCount( string strDirectory, string strFilter ) {
int nFiles = Directory.GetFiles( strDirectory, strFilter ).Length;
foreach( String dir in Directory.GetDirectories( strDirectory ) ) {
nFiles += GetNumberOfFiles(dir, strFilter);
}
return nFiles;
}
Хотя решение Джона может быть лучше.
Автор: Huppie Размещён: 26.08.2008 08:581 плюс
У меня есть приложение, которое генерирует количество каталогов и файлов в родительском каталоге. Некоторые из каталогов содержат тысячи подкаталогов с тысячами файлов в каждом. Чтобы сделать это, поддерживая отзывчивый пользовательский интерфейс, я делаю следующее (отправляя путь к методу ADirectoryPathWasSelected ):
public class DirectoryFileCounter
{
int mDirectoriesToRead = 0;
// Pass this method the parent directory path
public void ADirectoryPathWasSelected(string path)
{
// create a task to do this in the background for responsive ui
// state is the path
Task.Factory.StartNew((state) =>
{
try
{
// Get the first layer of sub directories
this.AddCountFilesAndFolders(state.ToString())
}
catch // Add Handlers for exceptions
{}
}, path));
}
// This method is called recursively
private void AddCountFilesAndFolders(string path)
{
try
{
// Only doing the top directory to prevent an exception from stopping the entire recursion
var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly);
// calling class is tracking the count of directories
this.mDirectoriesToRead += directories.Count();
// get the child directories
// this uses an extension method to the IEnumerable<V> interface,
// which will run a function on an object. In this case 'd' is the
// collection of directories
directories.ActionOnEnumerable(d => AddCountFilesAndFolders(d));
}
catch // Add Handlers for exceptions
{
}
try
{
// count the files in the directory
this.mFilesToRead += Directory.EnumerateFiles(path).Count();
}
catch// Add Handlers for exceptions
{ }
}
}
// Extension class
public static class Extensions
{
// this runs the supplied method on each object in the supplied enumerable
public static void ActionOnEnumerable<V>(this IEnumerable<V> nodes,Action<V> doit)
{
foreach (var node in nodes)
{
doit(node);
}
}
}
Автор: DraxReaper
Размещён: 17.04.2012 03:16
0 плюса
Кто-то должен сделать итерационную часть.
AFAIK, такого метода уже нет в .NET, поэтому я предполагаю, что кто-то должен быть тобой.
Автор: Lasse Vågsæther Karlsen Размещён: 26.08.2008 08:44Вопросы из категории :
- c# Преобразовать десятичную в двойную?
- c# Как рассчитать чей-то возраст в C #?
- c# Как вы сортируете словарь по значению?
- c# В чем разница между int и Integer в Java и C #?
- c# Как создать новый экземпляр объекта из Типа
- c# Datatable против Dataset
- file Как получить список каталогов в C?
- file Динамически загружать файл JavaScript
- file Find number of files with a specific extension, in all subdirectories
- file Поиск и замена строки в файле в Python
- file Как я могу получить путь и имя файла, который выполняется в настоящее время?
- file Как проверить, является ли данная строка допустимым / допустимым именем файла в Windows?
- recursion Что такое хвостовая рекурсия?
- recursion Рекурсия или итерация?
- recursion Путь от рекурсии к итерации
- recursion Рекурсивный список файлов в CLI Linux с указанием пути относительно текущего каталога
- recursion Что такое оптимизация вызовов?