How to check if UNC folder exists

  • Thread starter robert d via AccessMonster.com
  • Start date
R

robert d via AccessMonster.com

How do I check if the following folder exists

\\ComputerName\c

Using Len(FolderName, vbDirectory) produces an error.
 
D

Dirk Goldgar

robert d via AccessMonster.com said:
How do I check if the following folder exists

\\ComputerName\c

Using Len(FolderName, vbDirectory) produces an error.

You'd want to use the Dir() function in there, but you're right: it
doesn't work. You could do this:

Dim vDummy As Variant

On Error Resume Next
vDummy = Dir(FolderName & "\")
If Err.Number = 0 Then
' The folder exists and can be accessed.
Else
' The folder doesn't exist, or could not be accessed.
End If

Or you could use the FolderExists method of the FileSystemObject:

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(FolderName) Then
' The folder exists.
Else
' It doesn't.
End If
 
J

John Nurick

Hi Dirk,

You'd want to use the Dir() function in there, but you're right: it
doesn't work. You could do this:

Dim vDummy As Variant

On Error Resume Next
vDummy = Dir(FolderName & "\")
If Err.Number = 0 Then
' The folder exists and can be accessed.
Else
' The folder doesn't exist, or could not be accessed.
End If

This doesn't distinguish between an empty folder and a non-existent one:

? Len(Dir("\\ANNEMARIE\FU\")) 'contains files, first is 2FILE.EXE
9
? Len(Dir("\\ANNEMARIE\FU\New Folder\")) 'empty folder
0
? Len(Dir("\\ANNEMARIE\FU\No folder of this name\")) 'no such folder
0
Or you could use the FolderExists method of the FileSystemObject:

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(FolderName) Then
' The folder exists.
Else
' It doesn't.
End If

I haven 't been able to catch this one out.
 
D

Dirk Goldgar

John Nurick said:
Hi Dirk,



This doesn't distinguish between an empty folder and a non-existent
one:

? Len(Dir("\\ANNEMARIE\FU\")) 'contains files, first is 2FILE.EXE
9
? Len(Dir("\\ANNEMARIE\FU\New Folder\")) 'empty folder
0
? Len(Dir("\\ANNEMARIE\FU\No folder of this name\")) 'no such folder
0

But John, that's not what I wrote. The code I posted is just checking
to see if there's an error trying to read the directory, not whether the
return value is an empty string.
 
J

John Nurick

But John, that's not what I wrote. The code I posted is just checking
to see if there's an error trying to read the directory, not whether the
return value is an empty string.

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.

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. Which to me makes the FileSystemObject approach seem simpler.
 
D

Dirk Goldgar

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.
 
J

John Nurick

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.

Me neither, at least in WinDOS file systems.
 
K

Ken Snell \(MVP\)

Dirk Goldgar said:
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 error disappears if you add the trailing \ character after the share
folder name.
 
D

Dirk Goldgar

Ken Snell (MVP) said:
This error disappears if you add the trailing \ character after the
share folder name.

Yes; see my revised code. But that's actually testing something else:
not whether \\<servername>\My Documents is a directory, but whether it
*contains* a directory. This works, though, because every Windows/DOS
directory contains two directory entries.
 
K

Ken Snell \(MVP\)

Dirk Goldgar said:
Yes; see my revised code. But that's actually testing something else:
not whether \\<servername>\My Documents is a directory, but whether it
*contains* a directory. This works, though, because every Windows/DOS
directory contains two directory entries.

Aha, I missed that revision...sorry. And I concur with your statement above.
 

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