File search problem

  • Thread starter Thread starter Anthony
  • Start date Start date
A

Anthony

I am using filesearch on a command button to search for a
specific file. The code below works but will return a
count of all files where any part of the file name matches
the search name. I would like the code to search for the
specific file only and return the search results for the
specific file.

Thanks for your help
Anthony


Set fs = Application.FileSearch

With fs
.NewSearch
.LookIn = "C:\download"
.MatchTextExactly = True
.FileName = "HFDWORKREQ.txt"

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 "TRANSFER FILE BEFORE YOU CONTINUE."
End If
End With
 
Hi Anthony,

Another route, given your statement of requirements, would be

pth$ = "C:\download\HFDWORKREQ.txt"
fnm$ = Dir(pth$)
if fnm$ <> "" Then
' file exists, so do whatever with it
' fnm is returned as (only) the file name, i.e. "HFDWORKREQ.txt"
Else
' file does not exist (in that folder)
End If

If you are trying to access a specific file in a known folder, Dir is
probably the way to go.

CD
 
This workes great thanks
Anthony
-----Original Message-----
Hi Anthony,

Another route, given your statement of requirements, would be

pth$ = "C:\download\HFDWORKREQ.txt"
fnm$ = Dir(pth$)
if fnm$ <> "" Then
' file exists, so do whatever with it
' fnm is returned as (only) the file name, i.e. "HFDWORKREQ.txt"
Else
' file does not exist (in that folder)
End If

If you are trying to access a specific file in a known folder, Dir is
probably the way to go.

CD




.
 
Back
Top