Problems with file searching

D

David Howlett

I am trying to test for the existance of files. For example, to see if the
file "analyst1.mdb" exists or not. The following code sets 'result' to true
even if the file "analyst1.mdb" does not exist. There is however a file
named "analyst10.mdb".


With Application.FileSearch
.NewSearch
.LookIn = "C:\temp"
.SearchSubFolders = False
.FileName = "analyst1.mdb"
.MatchTextExactly = True
result = (.Execute > 0)
end with



I tried to write a new function:



Function DoesFileExist(f)
Dim sourcefile

On Error GoTo cantOpen
sourcefile = FreeFile
Open sourcefile For Binary Access Read As sourcefile
GoTo theEnd

cantOpen:
DoesFileExist = False
Exit Function

theEnd:
DoesFileExist = LOF(sourcefile) > 0
Close sourcefile
End Function




But it always returns false! lof is always 0, even if the file exists.

Can someone help?
 
D

damien

Can't you just do this with the Dir() function ?

ie
Function FileExists(filepath As String) As Boolean

FileExists = Dir(filepath) <> ""

End Function

Don't know if I'm over-simplyfying what you want to do.

Let me know how you get on.


Damien
 
R

Ron Weiner

Have you taken a look at the lowly DIR() function?

if len(dir("C:\temp\analyst1.mdb")) > 0 then
Msgbox "File is there"
else
Msgbox "Sorry file NOT there"
endif

Ron W
 

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