I do not quite understand what you mean by:
[...] change the "*.jpg", "*.mp4", "*.doc"
part by a variable [...]
But, I understand that you want to list certain types of files on a specific path, including their subfolders.
As far as I understand, and I have tried, the FileSystem.GetFiles()
method does not accept multiple file types in the wildcards
parameter, to filter the list of files, then one solution would be to create an array with the list of types it wants filter and call the FileSystem.GetFiles()
method multiple times, for each file type.
It would look like this:
Dim wildcards As String() = {"*.jpg",
"*.mp4",
"*.doc"}
For Each wildcard As String In wildcards
For Each arquivo In FileIO.FileSystem.GetFiles(caminho,
FileIO.SearchOption.SearchAllSubDirectories,
wildcard)
' Faz algo com o arquivo retornado.
Next
Next
Editing
Answering the question change, your variable can be the Array
I suggested in the first version of the answer:
Dim curingas As String() = {"*.jpg",
"*.mp4",
"*.doc"}
Or it could be a List
, which makes it easy to remove or add extensions:
Dim curingas As New List(Of String)({"*.jpg",
"*.mp4",
"*.doc"})
' Remove algumas extensões da lista.
curingas.Remove("*.jpg")
curingas.Remove("*.doc")
' Adiciona novas extensões à lista.
curingas.Add("*.mkv")
curingas.Add("*.avi")
Dim curingasStr As String = "*.jpg|*.mp4|*.doc"
Dim curingasArray = curingasStr.Split("|")
But, as I said earlier, the String
method does not accept multiple file types in the FileSystem.GetFiles()
parameter, so you could encapsulate the method call, something like this:
Public Function ObterArquivos(caminho As String,
curingas As List(Of String)
) As List(Of String)
Dim arquivosRetorno As New List(Of String)
For Each curinga As String In curingas
For Each arquivo In FileIO.FileSystem.GetFiles(caminho,
FileIO.SearchOption.SearchAllSubDirectories,
curinga)
arquivosRetorno.Add(arquivo)
Next
Next
Return arquivosRetorno
End Function
And it might even have an overload ( overload ) version of the function, to use the extensions variable in type wildcards
, as you wanted. This version would transform the variable String
with the extensions into a String
and call the original version of the function:
' Versão de sobrecarga (overload) da função ObterArquivos().
Public Function ObterArquivos(caminho As String, curingas As String) As List(Of String)
Dim curingasArray As String() = curingas.Split("|")
Dim curingasLista As List(Of String) = curingasArray.ToList()
Return ObterArquivos(caminho, curingasLista)
End Function