How can I exchange the part ( "*.jpg", "*.mp4", "*.doc") for a variable?

Asked

Viewed 51 times

0

How can I change the part "*.jpg", "*.mp4", "*.doc" by a variable? I tried some things like: Dim var As String = "*.jpg, *.mp4, *.doc" or Dim var As String = ".jpg .mp4 .doc", ..., but I couldn’t.

For Each arquivo In FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchAllSubDirectories, "*.jpg", "*.mp4", "*.doc")
        'value
    Next

Edit:

What I’d like to do is something similar:

Dim var As String = "*.mkv, *.mp4, *.avi"

And every time I need to use:

FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchAllSubDirectories, "*.jpg", "*.mp4", "*.doc")

I just do:

FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchAllSubDirectories,var)

So that when I have to change one of the extensions I don’t have to change one by one every time I have used the FileIO.FileSystem.GetFiles...

1 answer

0


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?

  • 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 use String even, with a separator, and make a Split() 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?

  • I edited the question.

  • 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 a List or even a String, as you were planning.

  • @Lucaspedro, I edited the answer, see if now answers your questions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.