Hello,
I have function that should query a directory and return files that match a pattern. However, the list contain duplicate elements. How do I remove duplicate element from list<string>
e,g searchPattern = cap|cap.gz
file = f1.cap, f2.cap.gz
* The code below will add the files twice
Public Function GetFilesByExtension(ByVal dir As String, ByVal searchPattern As String) As String()
Dim filelist As New List(Of String)
Dim m_arExt() As String = Split(searchPattern, "|")
For Each FileExt As String In m_arExt
filelist.AddRange((IO.Directory.GetFiles(dir, FileExt)))
Next
return Return filelist.ToArray
It seems like the simplest solution would be to avoid adding the duplicates in the first place.
For Each FileExt As String In m_arExt For Each file As String In IO.Directory.GetFiles(Dir, FileExt) If Not filelist.Contains(file) Then filelist.Add(file) End If Next Next
Open in new window