Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemObject - File Search Question 1

Status
Not open for further replies.

wraygun

Programmer
Dec 9, 2001
272
US
I'm using the following code to search for keywords in a filename, then populating a listbox. This can take quite a while if the user decides to search the entire hard drive. My question is: How can I stop this search programatically without raising an error?


Code:
Private Function FindFile(ByVal sFol As String, sFile As String) As Long
  Dim tFld As Folder, tFil As File, FileName As String, Cnt As Long

  Set fld = fso.GetFolder(sFol)
  For Each tFil In fld.Files
    For Cnt = 0 To 59
      If InStr(1, tFil.Name, WordToScan(Cnt), vbTextCompare) Then
      If InStr(1, tFil.Name, "dll", vbTextCompare) = False And InStr(1, tFil.Name, "mcr", vbTextCompare) = False And InStr(1, tFil.Name, "dle", vbTextCompare) = False Then
    List1.AddItem fld.Path & "\" & tFil.Name
      End If
      End If
       Next
      
      y = y + 1
      Label2.Caption = fld.Path & "/" & tFil.Name
    DoEvents
  Next
  
  If fld.SubFolders.Count > 0 Then
    For Each tFld In fld.SubFolders
      DoEvents
      FindFile = FindFile + FindFile(tFld.Path, sFile)
      
    Label4.Caption = y
    Next

Thanks,
Harold

***You can't change your past, but you can change your future***
 
I'm assuming that you want to have a "Cancel Search" button on the form. Assuming I understand what you're trying to do, in that button's event handler, just set some sort of variable, say CancelSearch, to true. Evaluate the variable inside your loop, and if it's true, exit the sub.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top