Vbscript / Operating System / Enumerate Computer Drives
List the drives attached to a computer and display the type and capacity of each.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk") For Each objDisk in colDisks strFreeSpace = byteValue(objDisk.Freespace) strTotalSpace = byteValue(objDisk.Size)
intDrivetype = objDisk.DriveType
Select Case intDriveType
Case 2 strDriveType = "Floppy Drive"
Case 3 strDriveType = "Hard Drive"
Case 4 strDriveType = "Network Drive"
Case 5 strDriveType = "CDROM Drive"
End Select
pctFreeSpace = int(100 * (objDisk.Freespace / objDisk.Size)) Wscript.Echo "Drive Letter: " & objDisk.DeviceId & vbCRLF & "Drive Type: " & strDriveType & vbCRLF & "Drive Size: " & strTotalSpace & vbCRLF & "Free Space: " & strFreeSpace & " (" & pctFreeSpace & "%)" Next
Function byteValue(val)
If val > 1000 Then
val = val/1024 unit = " KB"
If val > 1000 Then
val = val/1024 unit = " MB"
If val > 1000 Then
val = val/1024 unit = " GB"
If val > 1000 Then
val = val/1024 unit = " TB"
End If End If End If End If
byteValue = int(val) & unit
End function
Please note that a disclaimer applies to any code on this page.
|