Number of files in C # with LINQ

Env : C #, VStudio 2013, 4.5 Framework, Winforms

Purpose : getting the number of files (number) in the folder and subfolder that correspond to the extensions stored in the string array. An extension array may be with a "." no. {".dat", "TXT", "tzd"}

What I have done so far : when I have a "." everything works in the array of extensions: {".dat", ". txt", ". msg"}

I tried Replace but always returned 0.

Working code (only if always with the character "." In the string array):

string[] ext= new string[] { ".txt", ".msg", ".dat" }; totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories) .Count(s => ext.Any(s1 => s1 == Path.GetExtension(s))); 

The code does not work (always return 0):

 string[] ext= new string[] { "txt", ".msg", "dat" }; totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories) .Count(s => ext.Any(s1 => s1 == Path.GetExtension(s).Replace(".", ""))); 
+6
source share
4 answers

The documentation for the Path.GetExtension method indicates that the return value is:

An extension of the specified path (including the period "."), Or null, or String.Empty.

The second block of code removes the "." . therefore, none of the elements will match. So go in reverse order with the list of extensions and just make sure the beginning of each extension has a "." . and then use your first block of code.

 string[] ext = new string[] {"txt", ".msg", ".dat" } .Select(x => x.StartsWith(".") ? x : "." + x) .ToArray(); 
+7
source

You need to replace the point . both in the ext array element and in the catalog element. You need to use .Replace with s1 , and also compare the extension of the array and file after removing the point from each.

 string[] ext= new string[] { "txt", ".msg", "dat" }; totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories) .Count(s => ext.Any(s1 => s1.Replace(".", "") == Path.GetExtension(s).Replace(".", ""))); 
+1
source

It does not work because you need to remove points from both extensions. Path.GetExtension always adds a point. But this is an extension that may contain extensions without it. Therefore, you need to remove the point from both if you want to compare them:

So instead

 ....Count(s => ext.Any(s1 => s1 == Path.GetExtension(s).Replace(".", ""))); 

 ....Count(s => ext.Any(s1 => si.Replace(".", "") == Path.GetExtension(s).Replace(".", ""))); 

But, as others have mentioned, correct an invalid source, change the array in which it contains only extensions with a dot. Then it is much more readable and effective.

You can also use Contains :

 ....Count(s => ext.Contains(Path.GetExtension(s))); 
+1
source

Then, the GetExtension () method always includes a ".", So you need to normalize the extension you are checking.

 string[] extensions = new string[] { "docx", ".msg", "pdf" }; var filesCount = Directory.EnumerateFiles(folderPath, "*.*", SearchOption.AllDirectories) .Count(filePath => extensions.Any(e => (e.StartsWith(".") ? e : "." + e) == Path.GetExtension(filePath))); 

But it is better to normalize the assembly only once at the beginning.

0
source

Source: https://habr.com/ru/post/987131/


All Articles