I didn’t quite understand what you meant by:
[...] change the part "*.jpg", "*.mp4", "*.doc"
by a variable [...]
but, I understand that you want to list certain file types from a specific path, including its subfolders.
From what I understand, and tested, the method FileSystem.GetFiles()
does not accept multiple file types in the parameter wildcards
, to filter the list of files, then a solution would be you create an array with the list of types you want to filter and call the method FileSystem.GetFiles()
multiple times for each file type.
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
Responding to the question change, your variable may be Array
which I suggested in the first version of the reply:
Dim curingas As String() = {"*.jpg",
"*.mp4",
"*.doc"}
Or 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")
Or could even be a String
even, as you initially wanted, it could be transformed into an array, using the character pipe (or any other) as separator:
Dim curingasStr As String = "*.jpg|*.mp4|*.doc"
Dim curingasArray = curingasStr.Split("|")
But, as I said earlier, the method FileSystem.GetFiles()
does not accept multiple file types in the parameter wildcards
, then you could encapsulate the call to the method, more or less 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 version (Overload) of the function, to use the extension variable in type String
, as you wanted. This version would transform the variable String
with the extensions in a List
and would 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
The problem is that I use these parts a few times, and I would like to do this I said to create a single control variable that when changed would change all at the same time. With this solution I would have to change a lot. But there is no way anyway?
– Lucas Pedro
What do you use multiple times? The variable with the file types? Your variable may be the array, or else you can use a
List
, or you can useString
even, with a separator, and make aSplit()
after. But, I would need to understand exactly what you want, which I still don’t quite understand. You have some other code snippet to illustrate how you want to use the variable?– Pedro Gaspar
I edited the question.
– Lucas Pedro
I read your question edition, but the answer remains basically the same. As I said, the
FileSystem.GetFiles()
does not accept a filter with multiple file extensions, so you would have to call this method for each extension you want to search. But, its variable could be the array that is in the code I suggested, which could even be aList
or even aString
, as you were planning.– Pedro Gaspar
@Lucaspedro, I edited the answer, see if now answers your questions.
– Pedro Gaspar