How to Search for a File in VB

Save

Microsoft Visual Basic programming language contains the functions that give you the ability to use Windows system information. It includes the methods for retrieving information about files and directories. The "My.Computer.FileSystem.GetFiles" Method returns a set of strings that represent the path names for the files. By using the "GetFiles" parameter in your Visual Basic program, you can define the name and path of the directory for working with the drives and files in user applications.

  • Click "Start" in Windows, and then click "Microsoft Visual Studio."

  • Click "File." Select "Open," and then double-click on the Visual Basic program in which you want to add the file search function.

  • Add the following code after the "Public Shared Function" line:

         Function FindFiles(path As String, SearchStr As String, 

    _

             FileCount As Integer, DirCount As Integer)

    Dim FileName As String ' Filename variable.

    Dim DirName As String ' SubDirectory Name.

    Dim dirNames() As String ' Buffer for directory name

    entries.

            Dim nDir As Integer      ' Number of directories in 

    this path.

            Dim i As Integer         ' For-loop counter.

    On Error GoTo sysFileERR

    If Right(path, 1) <> "\" Then path = path & "\"

    ' Search for subdirectories.

    nDir = 0

    ReDim dirNames(nDir)

    DirName = Dir(path, vbDirectory Or vbHidden Or

    vbArchive Or vbReadOnly _

      Or vbSystem)  ' Even if hidden, and so on.

    Do While Len(DirName) > 0

    ' Ignore the current and related directories.

    If (DirName <> ".") And (DirName <> "..") Then

    ' Check for directory with bitwise comparison.

    If GetAttr(path & DirName) And vbDirectory

    Then

                     dirNames(nDir) = DirName

    DirCount = DirCount + 1

    nDir = nDir + 1

    ReDim Preserve dirNames(nDir)

    End If '

    directories.

         sysFileERRCont:

    End If

    DirName = Dir() ' Get next subdirectory.

    Loop

    ' Search through this directory.

    FileName = Dir(path & SearchStr, vbNormal Or

    vbHidden Or vbSystem _

            Or vbReadOnly Or vbArchive)

    While Len(FileName) <> 0

    FindFiles = FindFiles + FileLen(path & FileName)

    FileCount = FileCount + 1

    FileName = Dir() ' Get next file.

    Wend

    For Each Text1.Text As String In

    My.Computer.FileSystem.GetFiles( _

    My.Computer.FileSystem.SpecialDirectories.MyDocuments, _

    FileIO.SearchOption.SearchAllSubDirectories, "*.dll")

    Listbox.Items.Add(Text1.Text)

    Next

            Dim SearchPath As String, FindStr As String

    Dim FileSize As Long

    Dim NumFiles As Integer, NumDirs As Integer

    FindStr = Text1.Text

    FileSize = FindFiles(SearchPath, FindStr, NumFiles,

    NumDirs)

            Text3.Text = NumFiles & " Files found in " & NumDirs 
    • 1 & _

           " Directories"
  • Click "File" followed by "Save" to add the file search function to your program.

References

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!