Efficent way of searching in array of strings for a pattern

Well, I have to look in a folder and I get in the variable FilePathList an array of strings with the absolute path and including the name of all the files in a folder.

Thats nice, and now I want an optimal way of determine wether or not one of those arrays contains the pattern: “.tar.gz.part” and here is my problem.

I can do that using a for and and an ismatch expression and cycling for every file but I would like to find a better solution.

It would be incredible if this worked but it does not:

FilePathList.Contains(“.tar.gz.part”) Basically my problem is that contains an many other string methods do not work with wilcards (*) so here I am… asking for a way with probably vbnet code that cycles throgh the array of paths searching for the pattern.

Thanks in advance if someone can solve this,

Hello @Pelayo_Celaya_Fernandez, you can use LINQ to quickly check if any element in the FilePathList array contains the pattern “.tar.gz.part”. Here’s how you can do it:

Dim containsPattern As Boolean = FilePathList.Any(Function(filePath) filePath.Contains(“.tar.gz.part”))

If containsPattern Then
’ The pattern “.tar.gz.part” was found in at least one of the file paths
’ You can add your code here for handling this case
Else
’ The pattern was not found in any of the file paths
’ You can add your code here for the case when the pattern is not found
End If

Cheers! :slight_smile:

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