Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enumerate hard disks, exclude partitions
#37
I use the following VBS code to get the hard drive information, suggesting an error, but I can run the VBS code directly and it is successful. Huh


Macro HD vbs
Code:
Copy      Help
str vbs=
;Set ws = CreateObject("Wscript.Shell")
;Set fso = CreateObject("Scripting.FileSystemObject")
;
;If LCase(Right(WSH.FullName, 11)) = "wscript.exe" Then
;;;;;ws.run "cscript.exe -nologo """ & WSH.ScriptFullName & """", 0
;;;;;WSH.Quit
;End If
;
;Dim arr()
;Set oExec = ws.Exec("cmd /c ""(echo;list disk|diskpart)|findstr /irc:""[0-9][0-9]*  *[tgmk]b"" "" ")
;
;Do While oExec.StdOut.AtEndOfStream <> true
;;;;;str = Trim(oExec.StdOut.ReadLine)
;;;;;num = CInt(Split(str, " ")(1))
;;;;;ReDim Preserve arr(num)
;;;;;If InStrRev(str, "*") >0 Then
;;;;;;;;;arr(num) = "GPT"
;;;;;Else
;;;;;;;;;arr(num) = "MBR"
;;;;;End If
;Loop
;
;Dim objWMI, colDisks
;Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
;Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
;For Each objDisk in colDisks
;;;;;s = s & "Disk" & objDisk.Index & ":" & vbTab
;;;;;s = s & "size:" & GetSize(objDisk.Size) & vbTab
;;;;;s = s & "type:" & objDisk.Caption & vbTab
;;;;;s = s & "Partition Table:" & arr(CInt(objDisk.Index)) & vbCrLf
;;;;;strDiskID = Replace(objDisk.DeviceID, "\", "\\")
;;;;;Set colPartitions = objWMI.ExecQuery _
;;;;;;;;;("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
;;;;;;;;;& " where AssocClass=Win32_DiskDriveToDiskPartition")
;;;;;For Each objPartition in colPartitions
;;;;;;;strPartId = objPartition.DeviceID
;;;;;;;Set colLogicalDisks = objWMI.ExecQuery _
;;;;;;;;;;;("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
;;;;;;;For Each objLogicalDisk in colLogicalDisks
;;;;;;;;;size = objLogicalDisk.Size
;;;;;;;;;free = objLogicalDisk.Freespace
;;;;;;;;;used = FormatNumber((size - free) / size * 100, 2, true) & "%"
;;;;;;;;;s = s & "Partition" & objLogicalDisk.DeviceID & vbTab
;;;;;;;;;s = s & "size:" & GetSize(size) & vbTab
;;;;;;;;;s = s & "Remaining:" & GetSize(free) & vbTab
;;;;;;;;;s = s & "Usage rate:" & used & vbCrLf
;;;;;;;Next
;;;;;Next
;;;;;s = s & vbCrLf
;Next
;Msgbox s
;
;
;Function GetSize(intSize)
;;;If intSize/1024/1024 > 1024 Then
;;;;;GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
;;;Else
;;;;;GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
;;;End If
;End Function

VbsExec vbs


Messages In This Thread
Enumerate hard disks, exclude partitions - by win - 09-08-2018, 09:34 PM
RE: Enumerate hard disks, exclude partitions - by win - 02-20-2019, 09:37 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)