[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 磁盘序号和分区表格式的vbs代码有点小问题,能否合并成一个正常的vbs?

2个类似功能的vbs代码,各自都有点小问题,能否合并成一个正常的vbs?
总体要求:磁盘序号顺序显示。并且分区表格式显示正确。
diskinfo.vbs,这个代码问题是,分区表GPT的会错误的识别成MBR。但是磁盘序列号显示正常,比如磁盘0、磁盘1、磁盘2....
  1. Main
  2. Sub Main()
  3.   Dim objWMI, colDisks
  4.   Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  5.   Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  6.   ' 获取硬盘最大序号
  7.   nIndexMax = 0
  8.   For Each objDisk in colDisks
  9.     If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
  10.   Next
  11.   ' 按序号取信息
  12.   For i = 0 To nIndexMax
  13.     For Each objDisk in colDisks
  14.       ' 只取该序号信息
  15.       If objDisk.Index = i Then
  16.         s = s & "硬盘" & objDisk.Index & ":" & vbTab
  17.         s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  18.         's = s & "型号:" & objDisk.Caption &  vbCrLf
  19.         s = s & "型号:" & objDisk.Caption & vbTab
  20.         s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  21.         strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  22.         Set colPartitions = objWMI.ExecQuery _
  23.             ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  24.             & " where AssocClass=Win32_DiskDriveToDiskPartition")
  25.         For Each objPartition in colPartitions
  26.           strPartId = objPartition.DeviceID
  27.           Set colLogicalDisks = objWMI.ExecQuery _
  28.               ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
  29.               & """}  where AssocClass=Win32_LogicalDiskToPartition")
  30.           For Each objLogicalDisk in colLogicalDisks
  31.             size = objLogicalDisk.Size
  32.             free = objLogicalDisk.Freespace
  33.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  34.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  35.             s = s & "大小:" & GetSize(size) & vbTab
  36.             s = s & "剩余:" & GetSize(free) & vbTab
  37.             s = s & "使用率:" & used & vbCrLf
  38.           Next
  39.         Next
  40.         s = s & vbCrLf
  41.       End If
  42.     Next
  43.   Next
  44.   WScript.Echo s
  45. End Sub
  46. ' 格式化
  47. Function GetSize(intSize)
  48.   If intSize/1024/1024 > 1024 Then
  49.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  50.   Else
  51.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  52.   End If
  53. End Function
  54. ' 获取指定硬盘的分区表类型(GPT/MBR)
  55. Function IsGPT(ByVal nDiskIndex)
  56.   IsGPT = "MBR"
  57.   Dim wso, sLogFile, sText
  58.   Set wso = CreateObject("WScript.Shell")
  59.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  60.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  61.   Call TxtFile(sLogFile, 1, -2, sText)
  62.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  63. End Function
  64. ' 对文本指定编码进行读写操作2
  65. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  66. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  67.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  68.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  69.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  70.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  71.     If fso.GetFile(FileName).Size > 0 Then _
  72.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  73.   End If
  74. End Sub
  75. ' 以管理员身份运行
  76. Sub RunAsAdmin()
  77.   Dim objItems, objItem, strVer, nVer
  78.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  79.   For Each objItem In objItems
  80.     strVer = objItem.Version
  81.   Next
  82.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  83.   If nVer >= 60 Then
  84.     Dim oShell, oArg, strArgs
  85.     Set oShell = CreateObject("Shell.Application")
  86.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  87.       For Each oArg In WScript.Arguments
  88.         strArgs = strArgs & " """ & oArg & """"
  89.       Next
  90.       strArgs = strArgs & " /ADMIN:1"
  91.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  92.       Set oShell = Nothing
  93.       WScript.Quit(0)
  94.     End If
  95.     Set oShell = Nothing
  96.   End If
  97. End Sub
复制代码

引导方式-硬盘格式.vbs,这个代码问题是:磁盘序列号排列会乱序。但是分区表格式显示正常。
  1. Set ws = CreateObject("Wscript.Shell")
  2. Set fso = CreateObject("Scripting.FileSystemObject")
  3. If LCase(Right(WSH.FullName, 11)) = "wscript.exe" Then
  4.     ws.run "cscript.exe -nologo """ & WSH.ScriptFullName & """", 0
  5.     WSH.Quit
  6. End If
  7. Dim arr()
  8. Set oExec = ws.Exec("cmd /c ""(echo;list disk|diskpart)|findstr /irc:""[0-9][0-9]*  *[tgmk]b"" "" ")
  9. Do While oExec.StdOut.AtEndOfStream <> true
  10.     str = Trim(oExec.StdOut.ReadLine)
  11.     num = CInt(Split(str, " ")(1))
  12.     ReDim Preserve arr(num)
  13.     If InStrRev(str, "*") >0 Then
  14.         arr(num) = "GPT"
  15.     Else
  16.         arr(num) = "MBR"
  17.     End If
  18. Loop
  19. Dim objWMI, colDisks
  20. Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  21. Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  22. For Each objDisk in colDisks
  23.     s = s & "硬盘" & objDisk.Index & ":" & vbTab
  24.     s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  25.     s = s & "型号:" & objDisk.Caption & vbTab
  26.     s = s & "分区表:" & arr(CInt(objDisk.Index)) & vbCrLf
  27.     strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  28.     Set colPartitions = objWMI.ExecQuery _
  29.         ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  30.         & " where AssocClass=Win32_DiskDriveToDiskPartition")
  31.     For Each objPartition in colPartitions
  32.       strPartId = objPartition.DeviceID
  33.       Set colLogicalDisks = objWMI.ExecQuery _
  34.           ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
  35.       For Each objLogicalDisk in colLogicalDisks
  36.         size = objLogicalDisk.Size
  37.         free = objLogicalDisk.Freespace
  38.         used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  39.         s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  40.         s = s & "大小:" & GetSize(size) & vbTab
  41.         s = s & "剩余:" & GetSize(free) & vbTab
  42.         s = s & "使用率:" & used & vbCrLf
  43.       Next
  44.     Next
  45.     s = s & vbCrLf
  46. Next
  47. 'Msgbox s
  48. WScript.Echo s
  49. Function GetSize(intSize)
  50.   If intSize/1024/1024 > 1024 Then
  51.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  52.   Else
  53.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  54.   End If
  55. End Function
复制代码

TOP

回复 2# ygqiang


    第26行代码替换为第一段代码的6—15行,第49行代码后添加第一段代码的41—42行

TOP

本帖最后由 ygqiang 于 2025-3-25 22:40 编辑

回复 3# buyiyang


    谢谢,初步测试成功。

TOP

本帖最后由 ygqiang 于 2025-3-25 23:00 编辑

回复 3# buyiyang


    修改后的代码。分区表类型2.vbs,测试结果如下。
能否进一步修改vbs代码,实现:
首先显示当前系统启动盘。然后按照顺序显示其它盘。
比如,总共4个磁盘。
如果当前启动盘是磁盘1,磁盘显示顺序就是1,0,2,3
如果当前启动盘是磁盘2,磁盘显示顺序就是2,0,1,3
谢谢

TOP

返回列表