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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search for a filename in all subfolders 1

Status
Not open for further replies.

Paco75

Programmer
Oct 11, 2001
239
US
Hi I got a Excel Marco. And I want to search a file ( a .exe) before running it. How can I do this?

thanks
 
I assume by "search a file" you mean search to see whether it exists. (If you meant search inside the file for a particular sequence of characters, post again with more information.)

Check the help files for "FileSearch Object"

Here is an example (hope it helps):

Code:
Sub TEST()

With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"
    .SearchSubFolders = True
    .FileName = "Chess.zip"
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles
    
    If .Execute() > 0 Then
        MsgBox "There were " & .FoundFiles.Count & _
            " file(s) found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    Else
        MsgBox "There were no files found."
    End If

End With

End Sub
 
Yes This was exactly what I was looking for! Thanks a lot :)
 
Zathras,

Thanks. I've tucked this one away for future reference. A simple bit of code but quite handy. Worth a star to me.

I seem to forget about the FSO; haven't used it much to date, but am trying to break out of my fossilized way of doing things.


Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top