Search Files From Folders or Directories in VB.Net through String

In this post, SKOTechlearn will describe the step by step process for Searching Files through Code in VB.Net for developers, If you want to develop an application which can Search all Files from Folders or Directories in VB.Net through String. We can list all files which contain that string which we use for find files from existing folder or directories or from all folders and Sub-folders.

Change ProgressBar Color and Change Look in Different Way in VB.Net

So here, we will proceed with following process.

  1. Search files from particular given path
  2. Search files from dynamic given path

These two points describe static or dynamic way to Search Files from Folders in VB.Net.

 Which types of controls, we need for File Search?
For simple understanding purpose, we drag a TextBox, ListBox and a Command Button. Through these controls, we proceed for further process.

TextBox: For Input Path.
ListBox: For Search Files' Result.
Command Button: For Searching Activity.

Let’s start with simple learning tips with SKOTechlearn.

  1. Search files from particular given path in Vb.Net:

Suppose you want to find those files which contains particular character or String. And want to describe a fix path with folders and subfolders. But first, take a look of controls adjustment for finding and listing.

How can we define File's Pattern in Vb.net?
Search Files Button with Code in VB.net
Button with Code

List all Search Files From Directories or Sub- Directories in VB.Net.


Then write following code inside Command button:
Private Sub SearchFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchFilesBtn.Click

    Dim MainFldr = "C:\SKOTech_Files"
    Dim SKfiles() As System.IO.FileInfo
    Dim FldrInfo As New System.IO.DirectoryInfo(MainFldr)
    Dim flpth As String
    flpth = ""
    ListBox2.Items.Clear()
    SKfiles = FldrInfo.GetFiles("*" & Searching_Txt.Text & "*.*", IO.SearchOption.AllDirectories)
    For Each MySearchfile In SKfiles
        flpth = ""
        flpth = MySearchfile.DirectoryName + "\" + MySearchfile.Name
        ListBox2.Items.Add(flpth)
        Application.DoEvents()
    Next
End Sub
After complete coding, run the application. Input searching value inside “Searching_Txt” textbox. And press “Search Files” button. You will find you searching value’s containing files will show in “ListBox2” like following.
Search_File_Output in VB.Net

Batch process to Create Folder and Copy Files in Specific Created Folders

2. Search files from dynamic given path in Vb.Net:

In this process, we have to need another input box like Textbox for browse path dynamic process. So first we have to write code for Folder browse path.

➤ First Go to Toolbox and find “FolderBrowserDialog” control.

➤ Second drag this control to Form.

➤ Then write code inside “Browse..” button which you add for browse directory like following.

Browse Folder Code:
Private Sub BrowsePath_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePath_Btn.Click
   If (FolderBrowserDlg1.ShowDialog() = DialogResult.OK) Then
        BrowsePath_Txt.Text = FolderBrowserDlg1.SelectedPath
   End If
End Sub

After that come to “Search Files” Button and make some changes of code inside its “Click events” which describe just starting of this post. Now take a look of this process code:
Private Sub SearchFilesBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchFilesBtn.Click
   If Directory.Exists(BrowsePath_Txt.Text) Then
   'put condition if Browse Path is not Blank or  is not exist
      Dim MainFldr = BrowsePath_Txt.Text
      'Show dynamic given path for search
      Dim SKfiles() As System.IO.FileInfo
      Dim FldrInfo As New System.IO.DirectoryInfo(MainFldr)
      Dim flpth As String
      flpth = ""
      ListBox2.Items.Clear()
      On Error GoTo notfound
        SKfiles = FldrInfo.GetFiles("*" & Searching_Txt.Text & "*.*", IO.SearchOption.AllDirectories)
        For Each MySearchfile In SKfiles
            flpth = ""
            flpth = MySearchfile.DirectoryName + "\" + MySearchfile.Name
            ListBox2.Items.Add(flpth)
            Application.DoEvents()
        Next
      notfound:
    Else
       MsgBox("Directory does not exist", vbExclamation)
  End If
End Sub

Just copy this code and paste it on your application’s control.

Note: If you do not change control’s ID according to given description, then change controls id Name from this code same as you mention on your Form.

How to Fix Google Chrome Not Loading?

Now, run application, and search whatever your files you want to find from particular directory or directories.
Dynamic Find Files in VB.net
Dynamic Find Files
So, there is the way to Search files from folders or directories through string in vb.net by SKOTechLearn Tips.

WebPage Design in ASP.Net with Image Guide Step by Step

0 comments: