Search files using Directory.GetFiles() with Multiple filters

Today in this article, we shall simple and easy technique of GetFiles from a Directory using Multiple Filters in C#.

We shall also see how to get files using multiple filters like adding multiple files Example: .txt or .jpg or .csv etc.

Today in this article, we will cover below aspects,

We shall be using SearchOption class to specify the filter criteria.

It comes with enum values like TopDirectoryOnly = 0 means your files will be searched only in the top directory.

When the enum AllDirectories = 1 is specified, it searches from the current directory and all its subdirectories.

Getting Files from a Given Folder Directory

Get all files from a directory,

var files = Directory.GetFiles(path)

The GetFiles method returns the names of files (including their paths) that match the specified search pattern in the specified directory.

Getting Files from a given Directory using file extension filter

Get all files from a directory,

var files = Directory.GetFiles(path, "*.*")

Get all files from a directory with .TXT extension only

var files = Directory.GetFiles(path, "*.txt*")

Get all files from a directory with .jpeg extension only,

var files = Directory.GetFiles(path, "*.jpeg*")

Getting All Files from a given Directory using multiple file extension filter

GetFiles() methods have overloaded methods that can be used to provide search options.

Using option SearchOption.AllDirectories gives you all the files Files from a Directory and its subdirectory.

If using Option SearchOption.TopDirectoryOnly gives all the files from Getting Files from a Top Directory.

SearchOption.AllDirectories

Gives all files with extension Example .txt or .dat from the given directory. (This option includes a subdirectory too).

private static string[] GetFiles(string path)
        {
            var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".txt") || s.EndsWith(".dat"));
            return files.ToArray();
        }



call directory getfiles with multiple filtersGetFiles from a Directory using Multiple Filters in C

SearchOption.TopDirectoryOnly

Gives all files with extension Example .txt or .dat from the given top directory alone.

             var files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly)
            .Where(s => s.EndsWith(".txt") || s.EndsWith(".dat"));

Regex pattern – Regular expression for valid filename

GetFiles method using regex pattern can be used to get the names of files (including their paths) that match the specified search pattern in the specified directory.

Example

Below Regex, expression gives us all files list which contain “_Insert_” word in the file names.

Regex Pattern

@"^._Insert_."

Regular expression for valid filename Getfiles CSharp

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.