How to check if UNC folder exists

Discussion in 'Microsoft Access Form Coding' started by robert d via AccessMonster.com, Jan 30, 2006.

  1. robert d via AccessMonster.com, Jan 30, 2006
    #1
    1. Advertisements

  2. robert d via AccessMonster.com

    Dirk Goldgar Guest

    "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)
     
    Dirk Goldgar, Jan 30, 2006
    #2
    1. Advertisements

  3. robert d via AccessMonster.com

    John Nurick Guest

    Hi Dirk,

    On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
    <> 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.
     
    John Nurick, Jan 30, 2006
    #3
  4. robert d via AccessMonster.com

    Dirk Goldgar Guest

    "John Nurick" <> wrote in message
    news:
    > Hi Dirk,
    >
    > On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
    > <> 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)
     
    Dirk Goldgar, Jan 30, 2006
    #4
  5. robert d via AccessMonster.com

    John Nurick Guest

    On Mon, 30 Jan 2006 08:57:19 -0500, "Dirk Goldgar"
    <> wrote:

    >
    >"John Nurick" <> wrote in message
    >news:
    >> On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
    >> <> 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.
     
    John Nurick, Jan 30, 2006
    #5
  6. robert d via AccessMonster.com

    Dirk Goldgar Guest

    "John Nurick" <> wrote in message
    news:
    > On Mon, 30 Jan 2006 08:57:19 -0500, "Dirk Goldgar"
    > <> wrote:
    >
    >>
    >> "John Nurick" <> wrote in message
    >> news:
    >>> On Mon, 30 Jan 2006 00:12:42 -0500, "Dirk Goldgar"
    >>> <> 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)
     
    Dirk Goldgar, Jan 30, 2006
    #6
  7. robert d via AccessMonster.com

    John Nurick Guest

    On Mon, 30 Jan 2006 13:46:42 -0500, "Dirk Goldgar"
    <> 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.
     
    John Nurick, Jan 30, 2006
    #7
  8. "Dirk Goldgar" <> wrote in message
    news:%...
    > 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>
     
    Ken Snell \(MVP\), Jan 30, 2006
    #8
  9. robert d via AccessMonster.com

    Dirk Goldgar Guest

    "Ken Snell (MVP)" <> wrote in message
    news:%
    > "Dirk Goldgar" <> wrote in message
    > news:%...
    >> 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)
     
    Dirk Goldgar, Jan 30, 2006
    #9
  10. "Dirk Goldgar" <> wrote in message
    news:%...
    > "Ken Snell (MVP)" <> wrote in message
    > news:%
    >> "Dirk Goldgar" <> wrote in message
    >> news:%...
    >>> 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>
     
    Ken Snell \(MVP\), Jan 31, 2006
    #10
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newbie

    Check if file exists

    Newbie, Nov 4, 2003, in forum: Microsoft Access Form Coding
    Replies:
    3
    Views:
    232
    Newbie
    Nov 4, 2003
  2. Ryan

    Check to see if Doc exists

    Ryan, Nov 14, 2003, in forum: Microsoft Access Form Coding
    Replies:
    7
    Views:
    971
    Assaf
    Nov 14, 2003
  3. Les
    Replies:
    2
    Views:
    507
  4. Allen Browne

    Re: how check if a query exists

    Allen Browne, Jul 20, 2004, in forum: Microsoft Access Form Coding
    Replies:
    2
    Views:
    362
    Allen Browne
    Jul 21, 2004
  5. S. Matthew

    From a form check if a name exists on a different table

    S. Matthew, Sep 9, 2004, in forum: Microsoft Access Form Coding
    Replies:
    2
    Views:
    177
    Allen Browne
    Sep 9, 2004
Loading...

Share This Page