Reputation: 4421
Using LINQ I would like to retrieve all files under a given directory that are less/greater than a specified file size.
I have the following code that returns a List at present:
public static List<string> getFs(string sDir)
{
var files = Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories)
.Where(s => s.ToLower().EndsWith(".psd"));
return files.ToList();
}
I found the following code courtesy of Monsieur Skeet that seems to access file size:
long diskSpace = (from directory in Directory.EnumerateDirectories(@"c:\")
from file in Directory.EnumerateFiles(directory)
select file)
.Sum(file => new FileInfo(file).Length);
How would I adapt the file size aspect of this into my existing code or is this the wrong approach in the context of what I have already?
Upvotes: 0
Views: 1492
Reputation: 59037
You just need to add an additional Where
... As an example:
public static List<string> getFs(string sDir)
{
var files = Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories)
.Where(s => s.ToLower().EndsWith(".psd"))
.Where(s => new FileInfo(s).Length > 10000);
return files.ToList();
}
Of course, you could also combine the two Where
clauses; I kept them separate for clarity. This is equally valid:
var files = Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories)
.Where(s => s.ToLower().EndsWith(".psd") && new FileInfo(s).Length > 1000000);
Upvotes: 4