Results 1 to 8 of 8
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Drives Collection (VB6)

    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 ...
    <font color=blue>' How come neither of the next lines is valid as an alternative?</font color=blue>
    ' Set objDrive = fso.Drives(strPath)
    ' Set objDrive = fso.Drives.Item(strPath)
    <font color=blue>' which would simplify this part to "With objDrive" ...</font color=blue>
    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
    -John ... I float in liquid gardens
    UTC -7ąDS

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Drives Collection (VB6)

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

  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Drives Collection (VB6)

    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:

    <code>Public Sub TestGetDrive(ByRef sDriveSpec As String)</code>

    <code> Dim fso As New FileSystemObject</code>
    <code> Dim d As Drive</code>

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

    <code> Set fso = Nothing</code>
    <code> Set d = Nothing</code>

    <code>End Sub</code>

    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

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Drives Collection (VB6)

    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

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Drives Collection (VB6)

    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

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Drives Collection (VB6)

    Thanks, Andrew.
    -John ... I float in liquid gardens
    UTC -7ąDS

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Drives Collection (VB6)

    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
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Drives Collection (VB6)

    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.... <img src=/S/sailing.gif border=0 alt=sailing width=25 height=25>

Posting Permissions

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