Results 1 to 5 of 5

Thread: [2005] Searching File Directory

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Bournemouth
    Posts
    63

    [2005] Searching File Directory

    Hey,
    I have a piece of code which seems to do an exact search. I have tried to change it so that it retrieves all files that have some part of the search topic in their name. So if i search by "Hello" I would receive a file called and "Hello", "Hello World" and also "Everybody say Hello". Can anyone help me change my search to do this apposed to just lloking for an exact phrase? My current code is;

    Code:
    Private Sub SearchForFiles(ByVal StartDir As String)
            Dim DirPath As String = Path.GetDirectoryName(StartDir)
            Dim FilePattern As String = Path.GetFileName(StartDir)
            FilePattern = "*" & FilePattern & "*"
            Dim TotalFiles As Integer = 0
            lstResults.Items.Clear()
            If Directory.Exists(DirPath) Then
                Dim di As New DirectoryInfo(DirPath)
                Dim fi() As FileInfo = di.GetFiles(FilePattern, SearchOption.AllDirectories)
                For Each f As FileInfo In fi
                    lstResults.Items.Add(f.FullName)
                    TotalFiles += 1
                Next
            Else
                lstResults.Items.Add("Invalid Start Directory")
            End If
            lstResults.Items.Add("Total Files = " & TotalFiles.ToString())
        End Sub
    
    
        Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Clic
            txtSearch.Text = "C:\Hello\" & txtSearch.Text & ".*"
            SearchForFiles(txtSearch.Text.Trim())
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Searching File Directory

    Is this what you are looking for?

    sStartDirectory = the top directory to search
    sSearchString = the string that you wish to find in the full path

    If you want the search string to appear only in the file name, it will have to be amended further.

    It returns an array of file paths which contain the search string.

    Code:
    Imports System.IO
     Private Function GetFilesContainingSearchString(ByVal sStartDirectory As String, _
                ByVal sSearchString As String) As String()
    
            Dim sAllFiles() As String = Directory.GetFiles(sStartDirectory, "*.*", SearchOption.AllDirectories)
            Dim sReturn() As String
            Dim iUBound As Integer
    
            Dim i As Integer
    
            For i = 0 To sAllFiles.GetUpperBound(0)
    
                If Strings.InStr(sAllFiles(i), sSearchString) > 0 Then
                    ReDim Preserve sReturn(iUBound)
                    sReturn(iUBound) = sAllFiles(i)
                    iUBound += 1
                End If
    
            Next
    
            Return sReturn
    
        End Function

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Bournemouth
    Posts
    63

    Re: [2005] Searching File Directory

    I havekind of inherited my code from a previous employee and been tasked to do this? Woud I need call to only your function? Or would I still use my existing code and then call the function at some point?

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Searching File Directory

    Are you searching the full file path or just the file name? I have amended the code to search for the file name only and also I have made sure that the search is not case sensitive. You could implement it like this:

    Code:
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
           Dim sArray() As String = GetFileNamesContainingSearchString("C:\Hello\", "Hello")
            Dim i, iUBound As Integer
    
            If IsNothing(sArray) = True Then
    
                lstResults.Items.Add("Total Files = 0")
    
            Else
    
                iUBound = sArray.GetUpperBound(0)
    
                For i = 0 To iUBound
                    lstResults.Items.Add(sArray(i))
                Next
    
                lstResults.Items.Add("Total Files = " & (iUBound + 1).ToString())
    
            End If
        End Sub
    
      Private Function GetFileNamesContainingSearchString(ByVal sStartDirectory As String, _
                ByVal sSearchString As String) As String()
    
            Dim sAllFiles() As String = Directory.GetFiles(sStartDirectory, "*.*", SearchOption.AllDirectories)
            Dim sReturn() As String
            Dim iUBound As Integer
    
            Dim i As Integer
    
            For i = 0 To sAllFiles.GetUpperBound(0)
    
                Dim sFileName As String = GetFileName(sAllFiles(i))
    
                If Strings.InStr(sFileName.ToLower, sSearchString.ToLower) > 0 Then
                    ReDim Preserve sReturn(iUBound)
                    sReturn(iUBound) = sFileName
                    iUBound += 1
                End If
    
            Next
    
            Return sReturn
    
        End Function
    
        Private Function GetFileName(ByVal sFilePath As String) As String
            'pass the full file path and returns the file name
            Dim iLastSlashPos As Integer
            Dim i As Integer
            Dim iLenFilePath As Integer
    
            iLenFilePath = sFilePath.Length
    
            For i = 1 To iLenFilePath
    
                If Strings.Mid(sFilePath, i, 1) = "\" Then
                    iLastSlashPos = i
                End If
    
            Next i
    
            Return Strings.Right(sFilePath, iLenFilePath - iLastSlashPos)
    
        End Function

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Bournemouth
    Posts
    63

    Re: [2005] Searching File Directory

    Excellent!! Works Like A Treat!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width