Directory Search for String

  • Thread starter Thread starter lcannon
  • Start date Start date
L

lcannon

Guest
I need help to modify this function to be able to search all text files in a folder under c:\files and get the file names of the files that has matches to the strings in Field 2 of tblsearch and add those filenames to a table tblfiles. In other words I need to search all text files for strings matching any of the strings in field2 and place those filenames that have matching strings into a table.


Function GetFilename(strDrive As String)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
DoCmd.SetWarnings False
DoCmd.SetWarnings True
Set rs = db.OpenRecordset("tblfiles")

' resume next, on some items, read permissions are not allowed...
On Error Resume Next
Dim FileNameFound As String, i As Integer

With Application.FileSearch
.FileName = "*.txt"
.SearchSubFolders = True
.LookIn = strDrive & "\"
.Execute
If .FoundFiles.Count >= 1 Then

For i = 1 To .FoundFiles.Count

rs.AddNew
rs!Location = .FoundFiles(i)
rs.Update
Next
End If
End With
rs.Close
Set db = Nothing
End Function
 
Try this instead...

Code:
Dim sPath As String
    Dim rs As Recordset
    
    sPath = "\\Server\Partition\Directory"

    Set rs = CurrentDb.OpenRecordset("tFiles", dbOpenTable)

    With Application.FileSearch
        .NewSearch
        .LookIn = sPath
        .FileName = "*.txt"
        .SearchSubFolders = False
        .TextOrProperty = "YourSearchString"
        .MatchTextExactly = False
        .FileType = msoFileTypeAllFiles 'A reference must be made to the db when selecting this option (for the first time).  Type ".FileType = " (without the quotes) and after you type the =, you will be prompted by Access if you want to make the needed reference.
        If .Execute() > 0 Then
            Dim i As Integer
            For i = 1 To .FoundFiles.Count
            rs.AddNew
            rs("FilePathName") = .FoundFiles(i)
            rs.Update
            Next i
        End If
    End With
    
    rs.Close
    Set rs = Nothing
You will have to run it twice since you have two different search strings.
 
or maybe just pass in c:\files in your original function? Not to hot on VB file handling, but seems to me that's all that really needs to be done.
 

Users who are viewing this thread

Back
Top Bottom