Request a file system for a list of files with specific attributes?

I would like to be able to request a folder (and subfolders) and get a list of files that meet certain criteria, according to certain attributes ... so, for example, all files that have:

in c: \ somefolder
file_extension = ".abc" the
size of the files between x and y KB
(file name, for example "% this" or file name, for example "%,%" and a file name not like "% somethingelse%"
modifieddate between date1 and date2

Is this possible with LINQ, and what will be the syntax?

+3
source share
1 answer

Yes. The syntax will look something like this:

var files = from file in new DirectoryInfo(@"c:\some_folder")
                            .GetFiles("*.abc", SearchOption.AllDirectories)

            let lengthInKb = file.Length / 1024D
            let name = file.Name
            let modifiedDate = file.LastWriteTime.Date

            where  (lengthInKb >= x && lengthInKb <= y)
                && (name.EndsWith("this") || name.Contains("that")) 
                && !name.Contains("somethingelse")
                && (modifiedDate >= date1 && modifiedDate <= date2)

           select file;
+2
source

All Articles