Diretory.EnumerateFiles() conditionally

Hello,

I want to get all files from a folder based on a specific pattern contained in the file name.

For this I use the next expression, but the syntax seems to be incorrect:

Directory.GetFiles(folderPath).Where(Function(x) x.Split("\"c).Last.ToString.Contains(str)).ToArray

How could I conditionally use the method Directory.EnumerateFiles() instead?
It seems to be more performant avoiding memory issues.

Hi @SONYC

Try this query:

Directory.EnumerateFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly)
    .Where(Function(x) Path.GetFileName(x).Contains(str))
    .ToArray()

Hope it helps!!

Perfect. Thank you @Parvathy.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.