0

I was wondering if there is any way in VB.NET where you can use logical operators with 1-dimensional strings.

This is part of my code, and ideally I want to be able to combine the two searches (e.g. file.GetFiles("*.mp4" And "*.wmv")):

                For Each f In file.GetFiles("*.mp4")
                    FileBrowser.Items.Add(f.Name, 5)
                    i = FileBrowser.FindItemWithText(f.Name).Index
                    FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
                    FileBrowser.Items.Item(i).Name = f.FullName
                Next
                For Each f In file.GetFiles("*.wmv")
                    FileBrowser.Items.Add(f.Name, 5)
                    i = FileBrowser.FindItemWithText(f.Name).Index
                    FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
                    FileBrowser.Items.Item(i).Name = f.FullName
                Next

Can it be done by using a string array or list?

Dog Lover
  • 618
  • 1
  • 6
  • 18
  • 1
    You could do `For Each f in file.GetFiles("*.mp4").Concat(file.GetFiles("*.wmv"))` – Blorgbeard Aug 19 '15 at 03:45
  • @Blorgbeard Thank you - that's a very helpful suggestion. Would that be the most effective / shortest way? – Dog Lover Aug 19 '15 at 03:47
  • You can't pass two filters to one `GetFiles` call, unfortunately. You could write your own function which would retrieve **all** files, then filter the list however you wanted.. – Blorgbeard Aug 19 '15 at 03:57
  • You could also create an array with the file types you want to look for and then loop through that array in an `For Each` loop. – Visual Vincent Aug 19 '15 at 13:45
  • @VisualVincent That's not a bad suggestion. Could you please demonstrate it in an answer? – Dog Lover Aug 19 '15 at 21:26

3 Answers3

1

If you put each file extension in an array you can just iterate through it for each extension, and the only thing you'd have to change when adding or deleting extensions is the array itself.

Dim LookForExts() As String = New String() {"*.mp4", "*.wmv", "*.mp3", "*.wav"} 'Add or remove file extensions here.
For Each ext In LookForExts
    For Each f In file.GetFiles(ext)
        FileBrowser.Items.Add(f.Name, 5)
        i = FileBrowser.FindItemWithText(f.Name).Index
        FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
        FileBrowser.Items.Item(i).Name = f.FullName
    Next
Next
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
0

For .NET 4.0 and later,

Dim files = Directory.EnumerateFiles("C:\path", "*.*", SearchOption.AllDirectories)
        .Where(Function(s) s.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) 
        OrElse s.EndsWith(".wmv",  StringComparison.OrdinalIgnoreCase))

For earlier versions of .NET,

Dim files = Directory.GetFiles("C:\path", "*.*", SearchOption.AllDirectories)
       .Where(Function(s) s.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)     
       OrElse s.EndsWith(".wmv", StringComparison.OrdinalIgnoreCase))

Note that 'Directory.EnumerateFiles()' is preferred over 'Directory.GetFiles()' due to performance improvements. Directory.GetFiles() method will work perfectly fine if your directory does not have a huge amount of files within.

Aaron Glover
  • 1,199
  • 2
  • 13
  • 30
0

You may consume regex in combination with Directory.EnumerateFiles. Try like this:

Regex re = new Regex("\.(mp4|wmv)$");
Dim filteredFiles = Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories).Where(Function(c) re.IsMatch(c))
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56