FileSearch gives unexpected result

S

Stephen Allen

The following code gives a found files count of 3 where only 1 file
exists.
set fs = Application.FileSearch
With fs
.NewSearch
.FileName = "EXCEL.exe"
.LookIn = "C:\Program Files"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
End With
fs.Execute

For r = 1 To fs.FoundFiles.Count
MsgBox fs.FoundFiles(r)
Next r


The MsgBox refers to the same file each time "C:\Program
Files\Micrsoft Office\Office\EXCEL.exe"

Any thoughts on how to refine the code to give the expected answer?
 
R

Rob van Gelder

Mine finds just one.
So I created a shortcut to Excel.exe. Now it finds two! This could be your
problem.
 
T

Tom Ogilvy

..MatchTextExactly = True
has no effect on how it matches filenames. This setting has to do with
looking for a string contained in the file itself. Filesearch does not do a
match text exactly, so you just need to loop through the results and see if
one of the results is the file you want.

sStr1 = "excel.exe"
set fs = Application.FileSearch
With fs
.NewSearch
.FileName = sStr1
.LookIn = "C:\Program Files"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
End With
fs.Execute

sStr2 = ""
For r = 1 To fs.FoundFiles.Count
if instr(len(fs.foundfiles(r))-8,fs.FoundFiles(r),sStr1,vbTextCompare)
then
sStr2 = fs.FoundFiles(r)
fr = r
end if
Next r
if sStr2 <> "" then
msgbox fs.FoundFiles(fr) & " was found"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top