Fuzz Evans
Fuzz Evans

Reputation: 2943

How can I retrieve all files sizes of a folder based on file name and/or extension?

I have a form where the user navigates to a specific folder. The folder they navigate to will have many folders inside it, and within several levels of folders there will be a zip file named "file.zip". So this means there are going to be many folders all which have inside them a "file.zip". I want to scan the chosen folder for all of these "file.zip" files. I will eventually be looking for only those "file.zip" files that have a size of 0kb or is empty. I imagine that I will need to use LINQ to return a list of the files in the parent folder like this:

 string[] files = Directory.GetFiles(txtbxOldFolder.Text)
                      .Select(f => Path.GetFiles(f))
                      .ToArray();

But I also need their respective sizes so I can later create a list of the directory of any zip files that is empty or shows a size of 0kb. I'm thinking that perhaps there is a way to return the directory AND the size of each file based on name (file.zip) so that I can later go through the array and create a log of the directories for those file.zips that are empty/have a size of 0kb.

Upvotes: 0

Views: 271

Answers (1)

Primary Key
Primary Key

Reputation: 1237

var directory = @"c:\myfolder";
var files = Directory.GetFiles(directory, "file.zip", SearchOption.AllDirectories)
.Select(name => new FileInfo(name))
.Where(f => f.Length == 0).ToArray();

Upvotes: 2

Related Questions