John Nurick said:
Maybe I've got the wrong end of the stick. But I tried the code you
posted and found that I can pass it UNC paths to nonexistent
subfolders of valid paths and still get to the point you commented
with ' The folder exists and can be accessed.
IOW an invalid UNC path doesn't necessarily raise an error.
Huh. You're right -- I didn't test hard enough. The error is raised if
the share doesn't exist, but not if the share is valid but the folder
doesn't exist.
So I then tried the Len(Dir(...)) stuff to see if a combination of
error trapping and checking the return value of Dir() could
distinguish between an empty folder and a nonexistent one. The answer
seems to be that you have to pass the vbDirectory flag and then check
the return value.
Yes, that's what I'm finding. Not only that, but it's even more
complicated. To my surprise, this ...
?Dir("\\<servername>\My Documents", vbDirectory)
.... where <servername> is valid and "My Documents" is a share on that
server, raises error 52.
This revised code seems to work, though I haven't made exhaustive tests:
'----- start of code -----
Function fncFolderExists(FolderPath As String) As Boolean
Dim strResult As String
On Error Resume Next
If Right(FolderPath, 1) = "\" Then
strResult = Dir(FolderPath, vbDirectory)
Else
strResult = Dir(FolderPath & "\", vbDirectory)
End If
fncFolderExists = (Len(strResult) > 0)
End Function
'----- end of code -----
That turns on the fact that each directory contains subdirectories (".",
and "..") that point to itself and its parent. Maybe there's some
circumstance where that isn't true, in which case this would fail, but I
haven't heard of such a circumstance.
Which to me makes the FileSystemObject approach
seem simpler.
Could be. But it used to be the case that system/network administrators
would often disable scripting, for security reasons, in which case one
wouldn't be able to create a FileSystemObject. Alternatively, there are
various path-related Windows API functions, but some of them aren't
necessarily available on all Windows systems.