Search code examples
vb.netlistboxwildcardfile-search

VB Wildcard filepath filesystem.dir match


Attempting to create a program that will find a folder in a directory based upon a wildcard search and result all files found to a listbox. I seem to be having some problems with it finding a folder that is like the input name from a textbox.

For a ridiculous example:
Textbox entry is "Cats and D"
Filename = "\Cats and Dogs Funnies\"
the file is filled with a bunch of great cat and dog pictures and videos (in separate sub folders) and I want to put the videos in a listbox as .fullname that is obtained when clicking a button

The hangup I seem to be having (besides being sloppy and somewhat new at this) is obtaining the found result that matches the wildcard' folder. Below is the stripped down version of my script. Any help would be greatly appreciated.

[ VB ]

Private Sub Button1_Click (sender As Object, e As EventArgs) Handles Button1.Click

Dim tbxCont, fipath As String  
Dim di As System.IO.DirectoryInfo  
Dim diar As System.IO.FileInfo

tbxCont = TextBox1.Text
fipath = "C:Users\RandomUser\Desktop\" 

If txbCont = Nothing Then  
MsgBox("Please provide a name",MsgBoxStyle.OkOnly)  
Exit Sub  
Else  
If FileSystem.Dir(fipath & txbCont.Substring(0, txbCont.Length - 4) & "*", FileAttribute.Directory) <> "" Then  
di = New System.IO.DirectoryInfo(fipath & txbCont.Substring(0, txbCont.Length - 4) & "*" & "\Videos\")
For Each diar In di.GetFiles("*", searchOption:=IO.SearchOption.AllDirectories)  
FileList.Items.Add(diar.FullName)  
Next  
Exit Sub
End If

End Sub

It does not seem to be using the wildcard to find a like file. Is there a better way I could be going about this? Or, possibly obtain the found result from what has been found?

Much Thanks.


Solution

  • I assume that Filename = "\Cats and Dogs Funnies\" means DirectoryName= "Cats and Dogs Funnies".

    You should first check whether the search pattern a user has provided can map to an existing Directory.
    If it does not, inform that the partial path entered can't be found and return.
    If it exists, add the "Videos" subfolder to the Path, list all the files this combined path contains and add the files FullName to a ListBox Control.

    (Of course, some error handling is needed, first of all to verify that the a directory filter using wildcards is actually returning the desired results).

    This is a possible solution:

    If TextBox1.Text.Trim().Length = 0 Then
        MsgBox("Please provide a name", MsgBoxStyle.OkOnly)
        Exit Sub
    End If
    
    Dim txtCont As String = "*" & TextBox1.Text & "*"
    Dim DirectoryBase As String = "C:\Users\RandomUser\Desktop\"
    
    Dim DirInfo As DirectoryInfo = New DirectoryInfo(DirectoryBase)
    Dim dInfoSearchFolder As DirectoryInfo =
        DirInfo.GetDirectories(txtCont, SearchOption.AllDirectories).FirstOrDefault
    
    If IsNothing(dInfoSearchFolder) Then
        'Notify that the search path was not found
    End If
    
    dInfoSearchFolder = New DirectoryInfo(Path.Combine(dInfoSearchFolder.FullName, "Videos"))
    FileList.Items.AddRange(dInfoSearchFolder.GetFiles().Select(Function(f) f.FullName).ToArray())