CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    5

    [RESOLVED] Step by Step File Searching

    I'm working on a PCAnywhere OLE Automation program and basically I want to be able to sift through the thousands of host files that we use to connect to remote machines.

    I came up with a slick and cheater way of sorting the file names in the network drive so that I can find them pretty easily:

    Code:
    Private Sub searchBox_Change()
        Dim tempStr As String
        Dim tempFType As String
    
        fileList.Path = "M:\cknapp\pcAW"
        tempStr = searchBox.Text + tempStr
        tempFType = "*.chf"
        fileList.Pattern = tempStr + tempFType
        fileList.Refresh
        MsgBox (fileList.Pattern)
    End Sub
    This basically uses the filelistbox to eliminate possibilities by refreshing the Pattern element of the fileList object.

    What I want it to do on top of this is search the subfolders that contain more remote files, is there an easy way to do this?
    Last edited by phimuskapsi; July 26th, 2007 at 01:17 PM. Reason: Wanted Notfications

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Step by Step File Searching

    This is a Recursive function that does what you want.

    Code:
    Option Explicit
    ' posted by Wokawidget
    
    Private Sub cmdSearch_Click()
      Dim colFiles	As Collection
      Dim objFile	 As File
      Dim lngIndex	As Long
      Screen.MousePointer = vbHourglass
      DoEvents
      Set colFiles = New Collection
      SearchFolders "D:\temp\", "*.jpg", True, colFiles
      For lngIndex = 1 To colFiles.Count
    	  Set objFile = colFiles.Item(lngIndex)
    	  lstfiles.AddItem objFile.ParentFolder & "\" & objFile.Name
      Next lngIndex
      Screen.MousePointer = vbDefault
    End Sub
    
    Public Sub SearchFolders(ByVal pstrFolder As String, ByVal pstrFileSearch As String, ByVal pblnSearchSubFolders As Boolean, ByRef pcolFiles As Collection)
      Dim objFolder		   As Folder
      Dim objSubFolder		As Folder
      Dim objSubFolders	   As Folders
      Dim objFile			 As File
      Dim objFiles			As Files
      Dim objFSO			  As FileSystemObject
      Set objFSO = New FileSystemObject
      If objFSO.FolderExists(pstrFolder) Then
    	Set objFolder = objFSO.GetFolder(pstrFolder)
    	Set objFiles = objFolder.Files
    	For Each objFile In objFiles
    		If objFile.Name Like pstrFileSearch Then
    			pcolFiles.Add objFile
    		End If
    	Next objFile
    	Set objFiles = Nothing
    	If pblnSearchSubFolders Then
    	  Set objSubFolders = objFolder.SubFolders
    	  For Each objSubFolder In objSubFolders
    		  SearchFolders objSubFolder.ParentFolder & "\" & objSubFolder.Name, pstrFileSearch, pblnSearchSubFolders, pcolFiles
    	  Next objSubFolder
    	  Set objSubFolders = Nothing
    	End If
    	Set objFolder = Nothing
      End If
      Set objFSO = Nothing
    End Sub

    a few years back
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    May 2007
    Posts
    5

    Re: Step by Step File Searching

    Not really what I was looking for, I understand what this does but a few issues:

    1) This takes forever to run
    2) I don't want a search button I want it to filter on the fly, basically I would like you to type like this:

    S - Returns all names beginning with S
    So - Returns all names with So
    Son - Returns all names with son

    Each time you add a letter it should filter down. The little cheater way works but not for subfolders. For now I'll just dump all the hosts into a central folder and eliminate this field....until I find a better way that is.

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Step by Step File Searching

    I am not sure if I understand your question correctly. But this is what I do in one of my applications
    Code:
        Dim sFileName As String
        Dim sPath As String
        sPath = "C:\Windows\"
        
        sFileName = Dir(sPath & Text1.Text & "*.*", vbDirectory)
        List1.Clear
        While sFileName <> ""
            List1.AddItem sFileName
            sFileName = Dir
        Wend
    This code actually checks Windows Folder for any files/directories whose name starts with the characters entered in the Text1. It will display all files and directories.

    Probably this is what you are looking for.

  5. #5
    Join Date
    May 2007
    Posts
    5

    Talking Re: Step by Step File Searching

    That was EXACTLY what I needed. I plan on expanding it to allow switching directories once I get that figured out (i have an idea) i'll post it up. Thanks for your help.

  6. #6
    Join Date
    Jul 2007
    Posts
    6

    Re: [RESOLVED] Step by Step File Searching

    ahh sweet, something i was looking for. thanks!

  7. #7
    Join Date
    May 2007
    Posts
    5

    Re: [RESOLVED] Step by Step File Searching

    Code:
    'Checks to see if content in searchBox changed, then calls the search'
    Private Sub searchBox_Change()
        sPath = "M:\cknapp\PCaw\"
        Call searchFunction
    End Sub
    
    Private Function searchFunction()
        'Pointer for user-interface, they can see what folder they are in'
        pathBox.Text = sPath
        sFileName = Dir(sPath & searchBox.Text & "*.*", vbDirectory)
        fileList.Clear
    
        'Adds a ..Back line item in the list at the top'
        fileList.AddItem "..Back"
           
        While sFileName <> ""
            fileList.AddItem sFileName
            sFileName = Dir
        Wend
    End Function
    
    Private Sub fileList_DblClick()
        'Since ..Back is being added before anything else it's index will always be 0'
        If fileList.Selected(0) = True Then
            sPath = "M:\cknapp\pcaw\"
            Call searchFunction
        End If
        If searchBox.Text = "" Then
            MsgBox ("You have not entered any search terms!")
        End If
        If ((Right$(sFileName, 4) <> ".chf")) Then
            sLastPath = sPath
            sPath = "M:\cknapp\PCaw\" & fileList.Text & "\"
            Call searchFunction
        Else
            'If it's not ..back that is selected and the item is dbl-clicked, then edit the remote'
            Call EditRemote
        End If
    End Sub
    Little additions I made to the code.

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

Featured