Results 1 to 6 of 6

Thread: drives on a computer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    17
    I need a routine that will simply output the current drives present on a computer. I need the output to display, C:, D: etc.
    easy!

    [Edited by bazza on 03-21-2000 at 07:14 AM]

  2. #2
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    there's an API for that...

    Declare Function GetLogicalDrives& Lib "kernel32" ()

    to use...

    dim varNoOfDrives as long

    varNoOfDrives = GetLogicalDrives()



  3. #3
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    here's a way:

    Code:
    Private Sub Command1_Click()
    Dim varNoOfDrives As Long
    Dim Counter As Integer
    Dim CompareTo
    Dim tmpDrvLet As String
    Dim lstDrives As String
    
    ' *** get the logical Drives
        varNoOfDrives = 0
        varNoOfDrives = GetLogicalDrives&()
        lstDrives = ""
    
        If varNoOfDrives = 0 Then
            MsgBox "No Logical Drives Found!", vbCritical, "Error"
            Exit Sub
        End If
    
        For Counter = 1 To 26
            CompareTo = (2 ^ (Counter - 1))
            If (varNoOfDrives And CompareTo) <> 0 Then
                tmpDrvLet = Chr(Counter + 64)           ' Build a drive letter
                tmpDrvLet = tmpDrvLet & ":"             ' Add the colon
                lstDrives = lstDrives & Chr(13) & tmpDrvLet
            End If
        Next Counter
    
        MsgBox lstDrives
    
    End Sub
    don't forget to declare GetLogicalDrives&() just like in my previous post!

    [Edited by KenX on 03-22-2000 at 12:34 AM]

  4. #4
    Guest
    or u use this code =)

    (past this into a module)
    Code:
    Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    Public Function vbGetDriveType(DrivePath As String) As String
    On Error Resume Next
    Dim drivetype As Long
    drivetype = GetDriveType(DrivePath)
    If drivetype = 1 Then vbGetDriveType = ""
    If drivetype = 2 Then vbGetDriveType = "REMOVEABLE"
    If drivetype = 3 Then vbGetDriveType = "HARD DISK"
    If drivetype = 4 Then vbGetDriveType = "NETWORK"
    If drivetype = 5 Then vbGetDriveType = "CD-ROM"
    If drivetype = 6 Then vbGetDriveType = "RAM-DRIVE": Exit Function
    vbGetDriveType = "UNKNOWN TYPE"
    End Function
    now you can step trought a loop from A till Z ...
    if vbGetDriveType returns "" then the drive does not exist.


    hope that helps,

    taLON

  5. #5
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    The direct way to solve your problem is use the API
    GetLogicalDriveStrings, which does exactly what you
    are asking for...I didn't know this API before...

    Declare:
    Code:
    Declare Function GetLogicalDriveStrings Lib "kernel32.dll" Alias
    "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As
    Long
    Note:
    Since each entry in the string takes
    four characters (three for the name and one for the null), we can "count by fours" going
    through the string until we reach the end. This frees us from worrying about nulls.

    Code:
    Dim drivenames As String  ' receives list of root names
    Dim thisdrive As String  ' buffer for one extracted root directory name
    Dim c As Long  ' counter variable
    Dim slength As Long  ' receives length of returned string
    
    ' Make enough room in the buffer to receive the drive names.
      drivenames = Space(255)  ' more than enough room
    ' Get the root directory names of all logical drives.
    slength = GetLogicalDriveStrings(255, drivenames)  ' drivenames now holds the list
    ' Count by fours to extract the names of each drive.
      For c = 1 To slength Step 4  ' loop with an increment of 4
        thisdrive = Mid(drivenames, c, 3)  ' extract a 3-character string X:\ (X is the drive letter)
        Debug.Print thisdrive  ' display the drive name
      Next c
    [Edited by KenX on 03-22-2000 at 04:04 AM]

  6. #6
    Lively Member
    Join Date
    Jun 1999
    Location
    East Anglia, England
    Posts
    73
    I had the same problem a few days ago and here was a solution posted by _bman_ I think.

    First add a refereance to Microsoft Scripting Runtime.

    Public Sub GetDrives()
    Dim fso As New Scripting.FileSystemObject
    Dim dr As Scripting.Drive

    For Each dr In fso.Drives
    Debug.Print dr
    Next
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width