Results 1 to 9 of 9
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DIR NUL on network drive (Word 2000)

    I've read the threads Search Network (Access 2000 VBA) <post:=636,555>post 636,555</post:> and Check for network (WinXP OXP) <post:=323,468>post 323,468</post:> and still can't understand an apparent anomaly in my use of the DIR function in my Word2000 VBA code:<pre>Sub test()
    ' host drive
    Debug.Print Len(Dir("c:" & "*.*")) '=12 (quantities may vary)
    Debug.Print Len(Dir("c:" & "nul")) '=3
    ' network drive
    Debug.Print Len(Dir("BbbBOOT80GB ©" & "*.*")) '=9 (quantities may vary)
    Debug.Print Len(Dir("BbbBOOT80GB ©" & "nul")) '=0 (?!!???!!!)
    End Sub
    </pre>

    Testing for the file NUL on my local drive C: returns "NUL" as expected.
    Testing for the file NUL on a network drive C: returns "", instead of "NUL" as expected.

    I realise that the DIR command is old hat, and I might be best advised to use something else (such as FSO).
    I'm using the code to check that the user has nominated a valid (i.e. existing) folder in a GUI form.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DIR NUL on network drive (Word 2000)

    Why not test if Dir(directorypath, vbDirectory) is an empty string?

    I know that you detest On Error Resume Next, otherwise you could have used

    Function ExistsFolder(FolderPath As String) As Boolean
    Dim intAttr As Integer
    On Error Resume Next
    intAttr = GetAttr(FolderPath)
    If Err = 0 Then
    ExistsFolder = (intAttr And vbDirectory)
    End If
    End Function

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: DIR NUL on network drive (Word 2000)

    Does it work if you test with a different file? I can't even create a file called Nul - I just get repeated errors.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: DIR NUL on network drive (Word 2000)

    NUL is a special file name, it always exists and can be used to test for existence of the directory.

    I have a number of BAT files with lines like
    <code>If Exist X:foldernameNUL goto Exists
    ' do something here to map a drive or create a folder or whatever
    Exists:
    </code>
    StuartR

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DIR NUL on network drive (Word 2000)

    "Nul" is a device name like Lpt1. It can be used to discard the output of an MS-DOS command, e.g.
    <code>
    COPY *.* C:Temp > NUL
    </code>
    copies all files from the current folder to C:Temp, and discards the output that would otherwise have been displayed on the screen.

    Since Nul is a reserved name, you cannot create a file or folder with that name (and if you'd succeed in doing it through trickery, you wouldn't be able to rename, move or delete it!)

    The Dir function in VBA allows the use of Nul: Dir("C:Nul") will return "Nul" even though there isn't actually a file of that name. So if Dir(folderpath & "Nul") returns "Nul", the folder exists, but if it returns an empty string, the folder doesn't exist. This doesn't always work for network paths, as Chris has found.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DIR NUL on network drive (Word 2000)

    >I know that you detest On Error Resume Next, otherwise you could have used
    I'm mellowing, OK? I am coming around to the view that On Error is OK for very low-level functions, implicitly within a developers library (utils.dot), to provide functionality missing by design from a language.
    I'm going to dig in my heels at Application Developers using On Error, though.

    Thanks.
    I have embedded a short test function that seems to return the results I would expect.

    <pre>Function blnExistsFolder(FolderPath As String) As Boolean
    Dim intAttr As Integer
    On Error Resume Next
    intAttr = GetAttr(FolderPath)
    If Err = 0 Then
    blnExistsFolder = (intAttr And vbDirectory)
    End If
    'Sub TESTblnExistsFolder()
    ' ' local drives
    ' Debug.Assert blnExistsFolder("c:") = True
    ' Debug.Assert blnExistsFolder("c:") = True
    ' ' local folders
    ' Debug.Assert blnExistsFolder("Cos") = True
    ' Debug.Assert blnExistsFolder("Cos") = True
    ' ' network drives
    ' Debug.Assert blnExistsFolder("Bbb") = False
    ' Debug.Assert blnExistsFolder("Bbb") = False
    ' Debug.Assert blnExistsFolder("BbbBOOT80GB ©") = True
    ' Debug.Assert blnExistsFolder("BbbBOOT80GB ©") = True
    ' ' network folders
    ' Debug.Assert blnExistsFolder("BbbBOOT80GB ©Dos") = True
    ' Debug.Assert blnExistsFolder("BbbBOOT80GB ©Dos") = True
    'End Sub
    End Function</pre>


  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DIR NUL on network drive (Word 2000)

    FWIW here is my previous version, using FSO without an On Error.
    I have disabled my old code and substituted a call to your function.
    I performed the same assertion test on the old function, then performed again with the call in place which is the equivalent of my assertions in my post of ten minutes ago.
    <pre>Public Function blnFolderExists(strFolderName As String) As Boolean
    ' Dim fso As FileSystemObject
    ' Set fso = CreateObject("Scripting.FileSystemObject")
    ' If fso.FolderExists(strFolderName) Then
    ' blnFolderExists = True
    ' Else
    ' blnFolderExists = False
    ' End If
    blnFolderExists = blnExistsFolder(strFolderName)
    End Function</pre>


  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DIR NUL on network drive (Word 2000)

    The original version could be shortened to

    Public Function blnFolderExists(strFolderName As String) As Boolean
    Dim fso As FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    blnFolderExists = fso.FolderExists(strFolderName)
    End Function

    or even

    Public Function blnFolderExists(strFolderName As String) As Boolean
    blnFolderExists = CreateObject("Scripting.FileSystemObject").FolderE xists(strFolderName)
    End Function

  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: DIR NUL on network drive (Word 2000)

    Thanks, Hans and Stuart - it's always nice to learn something useful! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •