本帖最后由 apang 于 2015-1-8 09:58 编辑
回复 4# ygqiang
把硬盘与分区关系对应起来,试了一下 WMIC 的 ASSOC,但搞之不定。如果确有此需要,可以试试 vbs- Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
- Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
-
- For Each objDisk in colDisks
- i = i + 1
- s = s & "硬盘" & i & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & 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 & "分区" & objLogicalDisk.DeviceID & vbTab
- s = s & "大小:" & GetSize(size) & vbTab
- s = s & "剩余:" & GetSize(free) & vbTab
- s = s & "使用率:" & used & vbCrLf
- Next
- Next
- s = s & vbCrLf
- Next
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- fso.OpenTextFile("Result.txt", 2, true).Write s
- CreateObject("WScript.Shell").Run "NotePad Result.txt"
-
- 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
复制代码 vbs有一处笔误,修改下。
顺带加一个bat,不过效率不咋地- @echo off & setlocal enabledelayedexpansion
- for /f "delims=g" %%a in ('cmd /u /cEcho,有') do set "Tab=%%a"
- set w=wmic diskdrive where "MediaType like 'fixed%%'"
- set "w=%w% get DeviceID^,Model^,Size"
-
- for /f "tokens=1*delims==" %%i in ('%w% /value ^| find "="') do (
- if /i "%%i" == "DeviceID" set /a n+=1
- set "%%i!n!=%%j"
- )
-
- set "w1=wmic path Win32_DiskDriveToDiskPartition get Antecedent^,Dependent"
- set "w2=wmic path Win32_LogicalDiskToPartition get Antecedent^,Dependent"
- (for /l %%i in (1 1 !n!) do (
- call :GetSize !Size%%i!000 size
- if !size! gtr 102400 (set /a size/=1024 & set "G=GB") else set "G=MB"
- set /p=硬盘%%i:%Tab%<nul
- set /p=大小:!size:~,-2!.!size:~-2!!G!%Tab%<nul
- set /p=型号:!Model%%i!<nul
- echo,
-
- for /f tokens^=4delims^=^" %%a in ('%w1% ^| find /i """!DeviceID%%i!"""') do (
- for /f tokens^=4delims^=^" %%b in ('%w2%^|find /i """%%a"""') do (
- set "w3=wmic logicaldisk where "Name='%%b'" get FreeSpace^,Size"
- for /f "tokens=1,2" %%c in ('!w3!^|findstr [0-9]') do (
- call :GetSize %%c000 free
- call :GetSize %%d000 size
- set /a used=size-free
- if !size! gtr 102400 (
- set /a size/=1024,used/=1024 & set "G=GB"
- ) else set "G=MB"
- set /a used=1000*used/size
- if "!used:~,-1!" == "" set "used=0!used!"
- set /p=分区%%b%Tab%<nul
- set /p=大小:!size:~,-2!.!size:~-2!!G!%Tab%<nul
- if !free! gtr 102400 (
- set /a free/=1024 & set "G=GB"
- ) else set "G=MB"
- set /p=剩余:!free:~,-2!.!free:~-2!!G!%Tab%<nul
- set /p=使用率:!used:~,-1!.!used:~-1!%%<nul
- echo,
- )
- )
- )
- echo,
- ))>Result.txt
- start "" NotePad Result.txt
- pause & exit/b
-
- :GetSize
- set "num=%1" & set "n=" & set "%2="
- for %%a in (16 8 4 2 1) do (
- if not "!num:~%%a!"=="" set /a n+=%%a & set "num=!num:~%%a!"
- )
- set "num=%1" & set /a n-=6
- for /l %%a in (!n! -1 1) do (
- set "s=!num:~,-%%a!"
- if "!s:~,1!" == "0" set "s=!s:~1!"
- set /a m=s%%1048576,s/=1048576
- set "%2=!%2!!s!"
- set "num=!m!!num:~-%%a!"
- )
- if not defined %2 (
- set "%2=!num:~,-1!"
- ) else if "!%2:~,1!" == "0" set "%2=!%2:~1!"
复制代码
|