How to search the exist of a file

C

Cherry

I have written the following code to check did the
file "test.tmp" existence in c:\, however, I found that if
there is a file named "test111.tmp", the result return
is "Found".
How can I check the existence of "test.tmp" and return
false if there is file "test111.tmp"??

Public Sub MAIN()
With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.SearchSubFolders = False
.FileName = "test.tmp"
.MatchTextExactly = True
If .Execute() > 0 Then
MsgBox "Found"
Else
MsgBox "Not Found"
End If
End With
End Sub
 
T

Tushar Mehta

How about:

Sub testIt()
MsgBox Dir("c:\temp\test.txt") <> ""
End Sub

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
K

Karl E. Peterson

Hi Tushar --

Pardon the intrusion over here. Slow day. <g> We tend to discourage that usage in
the VB groups, because it interferes with potential Dir() loops that may already be
in progress. A more robust test uses the GetAttr function:

Public Function IsFile(SpecIn As String) As Boolean
Dim Attr As Long

' Guard against bad SpecIn by ignoring errors.
On Error Resume Next

' Get attribute of SpecIn.
Attr = GetAttr(SpecIn)
If Err.Number = 0 Then
' No error, so something was found.
' If Directory attribute set, then not a file.
If (Attr And vbDirectory) = vbDirectory Then
IsFile = False
Else
IsFile = True
End If
End If
End Function

Later... Karl
 
T

Tushar Mehta

Good point.

Hopefully, the OP is still around. <g>

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

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