PC Review


Reply
Thread Tools Rate Thread

How to check if UNC folder exists

 
 
robert d via AccessMonster.com
Guest
Posts: n/a
 
      30th Jan 2006
How do I check if the following folder exists

\\ComputerName\c

Using Len(FolderName, vbDirectory) produces an error.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...oding/200601/1
 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      30th Jan 2006
"robert d via AccessMonster.com" <u6836@uwe> wrote in message
news:5b1d2c6838ab1@uwe
> 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


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
 
 
 
John Nurick
Guest
Posts: n/a
 
      30th Jan 2006
Hi Dirk,

On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
<(E-Mail Removed)> wrote:

>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.
--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      30th Jan 2006
"John Nurick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> Hi Dirk,
>
> On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
> <(E-Mail Removed)> wrote:
>
>> 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


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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      30th Jan 2006
On Mon, 30 Jan 2006 08:57:19 -0500, "Dirk Goldgar"
<(E-Mail Removed)> wrote:

>
>"John Nurick" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)
>> On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
>> <(E-Mail Removed)> wrote:
>>
>>> 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

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


--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      30th Jan 2006
"John Nurick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> On Mon, 30 Jan 2006 08:57:19 -0500, "Dirk Goldgar"
> <(E-Mail Removed)> wrote:
>
>>
>> "John Nurick" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)
>>> On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
>>> <(E-Mail Removed)> wrote:
>>>
>>>> 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

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


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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      30th Jan 2006
On Mon, 30 Jan 2006 13:46:42 -0500, "Dirk Goldgar"
<(E-Mail Removed)> wrote:

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

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

 
Reply With Quote
 
Ken Snell \(MVP\)
Guest
Posts: n/a
 
      31st Jan 2006
"Dirk Goldgar" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.

--

Ken Snell
<MS ACCESS MVP>


 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      31st Jan 2006
"Ken Snell (MVP)" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)
> "Dirk Goldgar" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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.


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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
Ken Snell \(MVP\)
Guest
Posts: n/a
 
      31st Jan 2006
"Dirk Goldgar" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Ken Snell (MVP)" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)
>> "Dirk Goldgar" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> 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.

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

--

Ken Snell
<MS ACCESS MVP>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UNC vs Mapped Drive - Mapped drive works but UNC does not =?Utf-8?B?U2F0eWE=?= Microsoft Dot NET 0 24th Jul 2006 07:34 PM
valid syntax for copy from unc to unc LarryG Microsoft Windows 2000 CMD Promt 3 3rd Nov 2003 09:47 PM
UNC File copy MUCH FASTER than mapped UNC drive copy? NTNEWS Microsoft Windows 2000 Networking 0 17th Sep 2003 07:01 AM
UNC File copy MUCH FASTER than mapped UNC drive copy? NTNEWS Microsoft Windows 2000 0 17th Sep 2003 07:01 AM
UNC File copy MUCH FASTER than mapped UNC drive copy? NTNEWS Microsoft Windows 2000 File System 0 17th Sep 2003 07:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:41 PM.