Results 1 to 3 of 3

Thread: Searching an entire directory

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    Exclamation

    I want to search an entire directory for a string, does anyone have a suggestion on how to do this in VB. The directory may contain RTF, Word, Excel, etc. documents. Thanks.

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    You can use the Dir function to retrieve all file names from a directory, and process them.
    Code:
    Dim sFileName As String
    
    sFileName = Dir("C:\My Documents\")
    
    Do Until sFileName = vbNullString
        Call ProcessFile(sFileName)
        sFileName = Dir
    Loop
    Then, you can define a ProcessFile sub which will, well, process the file.
    Code:
    Sub ProcessFile(ByVal sFileName As String)
        Dim sFile As String
        Dim btFileNum As Byte
        
        btFileNum = FreeFile
        Open sFileName For Input As btFileNum
            sFile = Input(LOF(btFileNum), btFileNum)
        Close btFileNum
        
        ' Replace :rolleyes: with the string you want to search in the files... This will do a case-insensitive search
        If InStr(1, sFile, ":rolleyes:", vbTextCompare) Then
            ' Whatever you want to do with it, if it contains the string...
            Call lstGoodFiles.AddItem(sFileName)
        Else
            ' Whatever you want to do with it, if it doesn't contain the string... (If anything)
            Call lstBadFiles.AddItem(sFileName)
        End If
    End Sub
    Enjoy!

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Midwest
    Posts
    35

    Talking Thanks!

    Tried it and it works with a modification. Must make sure that the directory name is passed along with the filename. Thanks a lot!!!

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