• Drives Collection (VB6)

    Author
    Topic
    #408750

    I’m working on this incomplete Function using the FSO to get a drive UNC. (Probably reinventing the wheel; the unwritten part of the function wil replace the drive letter with the UNC if applicable.) What I don’t understand is why I first have to set the Drives Collection, how come I can’t go straight to the Drive Item?

    Function FileUNC(strPath as String) as String
    Dim fso As FileSystemObject
    Dim collDrives As Drives
    Dim objDrive As Drive

    Set fso = CreateObject(“Scripting.FileSystemObject”)

    If fso.FileExists(strPath) Then
    strFilePath = Left(fso.GetAbsolutePathName(strPath), 1)
    Set collDrives = fso.Drives ‘ if I set the collection here the code works, but …
    ‘ How come neither of the next lines is valid as an alternative?
    ‘ Set objDrive = fso.Drives(strPath)
    ‘ Set objDrive = fso.Drives.Item(strPath)
    ‘ which would simplify this part to “With objDrive” …
    With collDrives(strPath)
    If .DriveType = 3 Then FileUNC = .ShareName
    End With
    Debug.Print FileUNC
    End If
    Set objDrive = Nothing
    Set collDrives = Nothing
    Set fso = Nothing
    End Sub

    Viewing 5 reply threads
    Author
    Replies
    • #865843

      Strictly a guess. DRIVES is not a collection but a method of the fso object that returns a collection of drives.

    • #865844

      Strictly a guess. DRIVES is not a collection but a method of the fso object that returns a collection of drives.

    • #865872

      According to one reliable source (VB and VBA In A Nutshell – Language Reference by Paul Lomax (O’Reilly)), the FSO Drives collection Item property returns a Drive object (as expected), but the Item property Key parameter must be the drive letter, not a numerical index: “This is an unusual collection, since the drive’s index value (its ordinal position in the collection) can’t be used; attempting to do so generates runtime error 5, ‘Invalid procedure call or argument.'” Further explanation for this flaky behavior not provided. Sample code using Item property to return a Drive object:

      Public Sub TestGetDrive(ByRef sDriveSpec As String)

      Dim fso As New FileSystemObject
      Dim d As Drive

      Set d = fso.Drives("C")
      ' Set d = fso.GetDrive(sDriveSpec)
      Debug.Print d.DriveLetter, d.Path, d.DriveType

      Set fso = Nothing
      Set d = Nothing

      End Sub

      Due to this unorthodox behavior, it is simpler to just loop thru the Drives collection using For Each… Next, or, for a single drive whose path is known, use GetDrive method. The attached text file shows some other examples of using FSO or API functions to get drive info, including UNC paths, etc.

      HTH

      • #865874

        PS: You may be getting error if using full path instead of just drive letter, or drive letter with colon and/or colon & backslash. All of these should work:

        Set d = fso.Drives(“C”)
        Set d = fso.Drives(“C:”)
        Set d = fso.Drives(“C:”)

        For some reason thought you were trying to use numerical index, which definitely will not work….

        HTH

      • #865875

        PS: You may be getting error if using full path instead of just drive letter, or drive letter with colon and/or colon & backslash. All of these should work:

        Set d = fso.Drives(“C”)
        Set d = fso.Drives(“C:”)
        Set d = fso.Drives(“C:”)

        For some reason thought you were trying to use numerical index, which definitely will not work….

        HTH

      • #865906

        VBA in a NutShell by Lomax was my reference for trying to figure it out, but I don’t know the FSO well. As you can see by the code, I’m trying to get the UNC from the first letter, the drive letter, from a string containing a full path and filename. I’ll try it from code you posted. Thanks for your help, Mark.

        Edit. FWIW, here’s what I ended up with.

        Public Function GetFileUNC(ByRef strFilePath As String) As String
        Dim fso As FileSystemObject
        Dim strDriveName As String

        Set fso = CreateObject(“Scripting.FileSystemObject”)
        With fso
        If .FolderExists(strFilePath) Or .FileExists(strFilePath) Then
        strDriveName = Left(.GetAbsolutePathName(strFilePath), 1)
        With .GetDrive(strDriveName)
        If .DriveType = 3 Then
        GetFileUNC = Replace(strFilePath, strDriveName & “:”, .ShareName, , , vbTextCompare)
        Else
        GetFileUNC = strFilePath
        End If
        End With
        End If
        End With
        Set fso = Nothing
        End Function

        • #866442

          John,

          Here is another possibility using WSH :

          Function ConvertToUNC(strPath As String)
          Dim WshNetwork, strDrive As String
          Set WshNetwork = CreateObject(“WScript.Network”)
          strDrive = Left(strPath, 2)
          Set oDrives = WshNetwork.EnumNetworkDrives
          For i = 0 To oDrives.Count – 1 Step 2
          If oDrives.Item(i) = strDrive Then
          ConvertToUNC = Replace(strPath, strDrive, oDrives.Item(i + 1))
          Exit Function
          End If
          Next
          End Function

          If you are just looking for the UNC of a drive letter, you need to include a semicolon , e.g ConvertToUNC(“F:”)

          Andrew

        • #866443

          John,

          Here is another possibility using WSH :

          Function ConvertToUNC(strPath As String)
          Dim WshNetwork, strDrive As String
          Set WshNetwork = CreateObject(“WScript.Network”)
          strDrive = Left(strPath, 2)
          Set oDrives = WshNetwork.EnumNetworkDrives
          For i = 0 To oDrives.Count – 1 Step 2
          If oDrives.Item(i) = strDrive Then
          ConvertToUNC = Replace(strPath, strDrive, oDrives.Item(i + 1))
          Exit Function
          End If
          Next
          End Function

          If you are just looking for the UNC of a drive letter, you need to include a semicolon , e.g ConvertToUNC(“F:”)

          Andrew

      • #865907

        VBA in a NutShell by Lomax was my reference for trying to figure it out, but I don’t know the FSO well. As you can see by the code, I’m trying to get the UNC from the first letter, the drive letter, from a string containing a full path and filename. I’ll try it from code you posted. Thanks for your help, Mark.

        Edit. FWIW, here’s what I ended up with.

        Public Function GetFileUNC(ByRef strFilePath As String) As String
        Dim fso As FileSystemObject
        Dim strDriveName As String

        Set fso = CreateObject(“Scripting.FileSystemObject”)
        With fso
        If .FolderExists(strFilePath) Or .FileExists(strFilePath) Then
        strDriveName = Left(.GetAbsolutePathName(strFilePath), 1)
        With .GetDrive(strDriveName)
        If .DriveType = 3 Then
        GetFileUNC = Replace(strFilePath, strDriveName & “:”, .ShareName, , , vbTextCompare)
        Else
        GetFileUNC = strFilePath
        End If
        End With
        End If
        End With
        Set fso = Nothing
        End Function

    • #865873

      According to one reliable source (VB and VBA In A Nutshell – Language Reference by Paul Lomax (O’Reilly)), the FSO Drives collection Item property returns a Drive object (as expected), but the Item property Key parameter must be the drive letter, not a numerical index: “This is an unusual collection, since the drive’s index value (its ordinal position in the collection) can’t be used; attempting to do so generates runtime error 5, ‘Invalid procedure call or argument.'” Further explanation for this flaky behavior not provided. Sample code using Item property to return a Drive object:

      Public Sub TestGetDrive(ByRef sDriveSpec As String)

      Dim fso As New FileSystemObject
      Dim d As Drive

      Set d = fso.Drives("C")
      ' Set d = fso.GetDrive(sDriveSpec)
      Debug.Print d.DriveLetter, d.Path, d.DriveType

      Set fso = Nothing
      Set d = Nothing

      End Sub

      Due to this unorthodox behavior, it is simpler to just loop thru the Drives collection using For Each… Next, or, for a single drive whose path is known, use GetDrive method. The attached text file shows some other examples of using FSO or API functions to get drive info, including UNC paths, etc.

      HTH

    • #868169

      You could always just use the API method:

      Private Declare Function WNetGetConnection Lib “mpr.dll” Alias “WNetGetConnectionA” (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
      Function GetUNCPath(strDrivePath As String) As String
      Dim strTemp As String
      Dim dwReturn As Long
      strTemp = Space(255)
      dwReturn = WNetGetConnection(strDrivePath, strTemp, 255)
      If dwReturn = 0 Then
      strTemp = Trim(strTemp)
      GetUNCPath = Left(strTemp, Len(strTemp) – 1)
      Else
      GetUNCPath = “Error”
      End If
      End Function

      So on my machine, GetUNCPath(“T:”) returns NasData which is the share path of our T: drive. I have it just returning ‘Error’ if something is wrong, but there is a bunch of specifics you can get from the error message, like ‘network is not present’, etc.

      Hope this helps…. sailing

    • #868170

      You could always just use the API method:

      Private Declare Function WNetGetConnection Lib “mpr.dll” Alias “WNetGetConnectionA” (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
      Function GetUNCPath(strDrivePath As String) As String
      Dim strTemp As String
      Dim dwReturn As Long
      strTemp = Space(255)
      dwReturn = WNetGetConnection(strDrivePath, strTemp, 255)
      If dwReturn = 0 Then
      strTemp = Trim(strTemp)
      GetUNCPath = Left(strTemp, Len(strTemp) – 1)
      Else
      GetUNCPath = “Error”
      End If
      End Function

      So on my machine, GetUNCPath(“T:”) returns NasData which is the share path of our T: drive. I have it just returning ‘Error’ if something is wrong, but there is a bunch of specifics you can get from the error message, like ‘network is not present’, etc.

      Hope this helps…. sailing

    Viewing 5 reply threads
    Reply To: Drives Collection (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: