R
robert d via AccessMonster.com
How do I check if the following folder exists
\\ComputerName\c
Using Len(FolderName, vbDirectory) produces an error.
\\ComputerName\c
Using Len(FolderName, vbDirectory) produces an error.
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
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.
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.
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.
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.
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.
Ken Snell (MVP) said:This error disappears if you add the trailing \ character after the
share folder name.
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.
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.