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]
Printable View
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]
there's an API for that...
Declare Function GetLogicalDrives& Lib "kernel32" ()
to use...
dim varNoOfDrives as long
varNoOfDrives = GetLogicalDrives()
:)
here's a way:
don't forget to declare GetLogicalDrives&() just like in my previous post!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
[Edited by KenX on 03-22-2000 at 12:34 AM]
or u use this code =)
(past this into a module)
now you can step trought a loop from A till Z ...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
if vbGetDriveType returns "" then the drive does not exist.
hope that helps,
taLON
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:
Note:Code:Declare Function GetLogicalDriveStrings Lib "kernel32.dll" Alias
"GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As
Long
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.
[Edited by KenX on 03-22-2000 at 04:04 AM]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
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