:: On Thu, 21 Apr 2022 18:35:22 -0700 (PDT)
:: (microsoft.public.vb.general.discussion)
:: <
f9fb1614-045d-44e7...@googlegroups.com>
:: Kim Hawker <
hawke...@gmail.com> wrote:
> I cannot figure what is wrong here. it all used to work. Now when I
> run similar code I get UN-expected results.
Kim, I'm happy you found the "typo" and fixed it, but please allow me
to ask you a question
> Public Function Check_File(sPath As String) As Boolean
> If Dir$(sPath) <> "" Then
> Check_File = True
> Else
> Check_File = False
> End If
now, the above uses the "Dir$()", fine, but what if there's a folder
(notice a FOLDER, not a file) called "Photo3.jpg" in the path you're
checking ? In such a case your function will tell you that "a file"
exists while that isn't a file at all; my suggestion is to use some
code like the following
Public Function Check_File(ByVal sPathName As String) As Boolean
Dim vAttr As VbFileAttribute
On Local Error Resume Next
CheckFile = False
Err.Clear
vAttr = GetAttr(sPathName)
If Err.Number <> 0 Then
Err.Clear
Exit Function
End If
If (vAttr And vbDirectory) Then
Exit Function
End If
CheckFile = True
End Function
it will be faster than the "Dir" and, if the specified pathname points
to a directory it will correctly return "false"