Sunday, January 20, 2019

Search files from folders or directories through string in VB.Net

Search files from folders or directories through string in vb.net

In this post, SKOTechlearn will describe the step by step process for developers, who want to develop an application which can Search all files from folders or directories through string in vb.net. 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 subfolders.

So here, we will proceed with following process.
  1. Search files from particular given path
  2. Search files from dynamic given path
 Which types of controls, we need for it?
For simple understanding purpose, we drag a TextBox, ListBox and a Command Button. Through these controls, we proceed for further process.

Let’s start with simple learning tips with SKOTechlearn.

  1. Search files from particular given path:
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.
Search Files Button with Code in VB.net
Button with Code
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

  2. Search files from dynamic given path:
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.

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.

0 comments: