RMu
RMu

Reputation: 869

Search file with multiple extension c#

I have some files that have multiple extensions(ex: D*.P*.C*) I'm building a process to move files with specific extensions like the above one and .csv and .arc files. I'm failing to filter D*.P*.C* files. Here is the code below. Any help will be greatly appreciated.

var entries =
    Directory.GetFileSystemEntries(_sourceLocation_FRUD, "*.*", SearchOption.AllDirectories)
             .Where(s => (s.StartsWith("D"))
                          && (s.Contains(".P"))
                          && (s.EndsWith(".C")));

Upvotes: 0

Views: 112

Answers (1)

Grant Winney
Grant Winney

Reputation: 66499

The second parameter isn't Regex, but it is a form of wildcard search.

Remove the LINQ code and specify your extension pattern in the method call. Since the * means 0 or more characters, you should be able to just use your D*.P*.C* pattern.

var entries =
    Directory.GetFileSystemEntries(_sourceLocation_FRUD, "D*.P*.C*", SearchOption.AllDirectories)

Assuming only the extension starts with D, not the full filename, you may have to change your pattern to *.D*.P*.C*

Upvotes: 4

Related Questions