Problems with file searching

  • Thread starter Thread starter David Howlett
  • Start date Start date
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?
 
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
 
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
 
Back
Top