+ Reply to Thread
Results 1 to 4 of 4

Display a list of all logical drives and include full paths for mapped drives

  1. #1
    brotherescott@yahoo.com
    Guest

    Display a list of all logical drives and include full paths for mapped drives

    I am trying to write some VBA code to list all the drives on my
    computer and if they are mapped show the full path they map to. I am
    trying to do this to help work across many computers better. I am
    trying to understand were some spreadsheet/macro users are getting some
    data from.

    Here is some code I got from the Microsoft Knowledge Base. It gives me
    the list of logical dirve letters on my PC but no path for the mapped
    drives.'

    Thanks
    Scott

    Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
    Alias "GetLogicalDriveStringsA" _
    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

    Private Function GetDriveStrings() As String
    ' Wrapper for calling the GetLogicalDriveStrings API

    Dim result As Long ' Result of our api calls
    Dim strDrives As String ' String to pass to api call
    Dim lenStrDrives As Long ' Length of the above string

    ' Call GetLogicalDriveStrings with a buffer size of zero to
    ' find out how large our stringbuffer needs to be
    result = GetLogicalDriveStrings(0, strDrives)

    strDrives = String(result, 0)
    lenStrDrives = result

    ' Call again with our new buffer
    result = GetLogicalDriveStrings(lenStrDrives, strDrives)

    If result = 0 Then
    ' There was some error calling the API
    ' Pass back an empty string
    ' NOTE - TODO: Implement proper error handling here
    GetDriveStrings = ""
    Else
    GetDriveStrings = strDrives
    End If
    End Function

    Private Sub CommandButton1_Click()
    Dim strDrives As String

    ' Find out what drives we have on this machine
    strDrives = GetDriveStrings()

    If strDrives = "" Then
    ' No drives were found
    MsgBox "No Drives were found!", vbCritical
    Else
    ' Walk through the string and list each drive
    DisplayDriveTypes strDrives
    End If
    End Sub

    Private Sub DisplayDriveTypes(drives As String)
    ' Walk through the logical drive string and display the drive
    ' letters. The logical drive string is a null seperated
    ' double null terminated string.

    Dim pos As Long
    Dim drive As String

    ListBox1.Clear
    pos = 1

    Do While Not Mid$(drives, pos, 1) = Chr(0)
    drive = Mid$(drives, pos, 3)
    pos = pos + 4
    ListBox1.AddItem UCase(drive)
    Loop
    End Sub


  2. #2
    Tom Ogilvy
    Guest

    RE: Display a list of all logical drives and include full paths for ma

    http://support.microsoft.com/default...;en-us;Q192689
    Q192689 - HOWTO: Get UNC Path From a Mapped Network Share's Drive Letter


    http://support.microsoft.com/default...;en-us;Q291573
    HOWTO: Use Visual Basic to List Active Logical Drives

    http://support.microsoft.com/default...;en-us;Q189667
    HOWTO: List the Drives in a System Using the FileSystemObject


    http://support.microsoft.com/default...;en-us;Q256847
    Q256847 - HOWTO: Use the WNetUseConnection API to Map a Drive in Visual Basic

    --
    Regards,
    Tom Ogilvy


    "brotherescott@yahoo.com" wrote:

    > I am trying to write some VBA code to list all the drives on my
    > computer and if they are mapped show the full path they map to. I am
    > trying to do this to help work across many computers better. I am
    > trying to understand were some spreadsheet/macro users are getting some
    > data from.
    >
    > Here is some code I got from the Microsoft Knowledge Base. It gives me
    > the list of logical dirve letters on my PC but no path for the mapped
    > drives.'
    >
    > Thanks
    > Scott
    >
    > Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
    > Alias "GetLogicalDriveStringsA" _
    > (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    >
    > Private Function GetDriveStrings() As String
    > ' Wrapper for calling the GetLogicalDriveStrings API
    >
    > Dim result As Long ' Result of our api calls
    > Dim strDrives As String ' String to pass to api call
    > Dim lenStrDrives As Long ' Length of the above string
    >
    > ' Call GetLogicalDriveStrings with a buffer size of zero to
    > ' find out how large our stringbuffer needs to be
    > result = GetLogicalDriveStrings(0, strDrives)
    >
    > strDrives = String(result, 0)
    > lenStrDrives = result
    >
    > ' Call again with our new buffer
    > result = GetLogicalDriveStrings(lenStrDrives, strDrives)
    >
    > If result = 0 Then
    > ' There was some error calling the API
    > ' Pass back an empty string
    > ' NOTE - TODO: Implement proper error handling here
    > GetDriveStrings = ""
    > Else
    > GetDriveStrings = strDrives
    > End If
    > End Function
    >
    > Private Sub CommandButton1_Click()
    > Dim strDrives As String
    >
    > ' Find out what drives we have on this machine
    > strDrives = GetDriveStrings()
    >
    > If strDrives = "" Then
    > ' No drives were found
    > MsgBox "No Drives were found!", vbCritical
    > Else
    > ' Walk through the string and list each drive
    > DisplayDriveTypes strDrives
    > End If
    > End Sub
    >
    > Private Sub DisplayDriveTypes(drives As String)
    > ' Walk through the logical drive string and display the drive
    > ' letters. The logical drive string is a null seperated
    > ' double null terminated string.
    >
    > Dim pos As Long
    > Dim drive As String
    >
    > ListBox1.Clear
    > pos = 1
    >
    > Do While Not Mid$(drives, pos, 1) = Chr(0)
    > drive = Mid$(drives, pos, 3)
    > pos = pos + 4
    > ListBox1.AddItem UCase(drive)
    > Loop
    > End Sub
    >
    >


  3. #3
    brotherescott@yahoo.com
    Guest

    Re: Display a list of all logical drives and include full paths for ma

    The first link you sent worked perfectly. I had searched the MSKB but
    did not understand the titles enough to look into some of them further.

    Thanks
    Scott


  4. #4
    Registered User
    Join Date
    12-02-2015
    Location
    Aberdeen, Scotland
    MS-Off Ver
    2010
    Posts
    1

    Re: Display a list of all logical drives and include full paths for mapped drives

    Saw the links were out of date, after another of Microsoft's reshuffles

    If you just google the codes - it should take you to: jeffpar.github.io

    Attempted to paste in the bookmarks myself but I don't have enough credit, so am not allowed

    Codes are:
    Q192689 - HOWTO: Get UNC Path From a Mapped Network Share's Drive Letter
    Q291573 - HOWTO: Use Visual Basic to List Active Logical Drives
    Q189667 - HOWTO: List the Drives in a System Using the FileSystemObject
    Q256847 - HOWTO: Use the WNetUseConnection API to Map a Drive in Visual Basic

    Here is a link to the entire archive: jeffpar.github.io/kbarchive

    Regards,
    Grant

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1