[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
第一个脚本不能达到功能吗?
第二个脚本是BAT+VBS混编。
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 5# ygqiang

检查多个硬盘是不是GPT分区,我这里不方便测试,请自行测试:
  1. @echo off
  2. for /f "tokens=1,2,* delims= " %%a in ('echo list disk^|diskpart^|find /i "B"') do (
  3.   (echo,%%c|find /i "*" >nul 2>nul) && (
  4.      echo %%a %%b 是GPT分区
  5.   ) || (
  6.      echo %%a %%b 不是GPT分区
  7.   )
  8. )
  9. pause
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 yu2n 于 2015-1-7 12:23 编辑

Windows XP 的 DiskPart 命令有 BUG ? 我在虚拟机建立了一块硬盘(Disk 1),分区表为 GPT,用PAProCn、DiskGenius 都可以识别为 GPT 分区表,但是 DiskPart 命令识别不出来:
  1. C:\Documents and Settings\Yu2n>ver
  2. Microsoft Windows XP [Version 5.1.2600]
  3. C:\Documents and Settings\Yu2n>diskpart
  4. Microsoft DiskPart version 5.1.3565
  5. Copyright (C) 1999-2003 Microsoft Corporation.
  6. On computer: PC-02
  7. DISKPART> list disk
  8.   Disk ###  Status      Size     Free     Dyn  Gpt
  9.   --------  ----------  -------  -------  ---  ---
  10.   Disk 0    Online        20 GB      0 B
  11.   Disk 1    Online        10 GB      0 B
  12.   Disk 2    Online       500 GB      0 B
  13. DISKPART>
复制代码
因为 DiskPart 不能正常工作,到此为止了……放个VBS代码上来,请自行测试吧:
2015.1.7 更新:感谢 apang 提醒 65 行 GetSize 少了个 S ,现已补上,谢谢。
  1. RunAsAdmin
  2. Main
  3. Sub Main()
  4.   Dim objWMI, colDisks
  5.   Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  6.   Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  7.   For Each objDisk in colDisks
  8.     s = s & "硬盘" & objDisk.Index & ":" & vbTab
  9.     s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  10.     s = s & "型号:" & objDisk.Caption & vbTab
  11.     s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  12.     strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  13.     Set colPartitions = objWMI.ExecQuery _
  14.         ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  15.         & " where AssocClass=Win32_DiskDriveToDiskPartition")
  16.     For Each objPartition in colPartitions
  17.       strPartId = objPartition.DeviceID
  18.       Set colLogicalDisks = objWMI.ExecQuery _
  19.           ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
  20.       For Each objLogicalDisk in colLogicalDisks
  21.         size = objLogicalDisk.Size
  22.         free = objLogicalDisk.Freespace
  23.         used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  24.         s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  25.         s = s & "大小:" & GetSize(size) & vbTab
  26.         s = s & "剩余:" & GetSize(free) & vbTab
  27.         s = s & "使用率:" & used & vbCrLf
  28.       Next
  29.     Next
  30.     s = s & vbCrLf
  31.   Next
  32.   WScript.Echo s
  33. End Sub
  34. ' 格式化
  35. Function GetSize(intSize)
  36.   If intSize/1024/1024 > 1024 Then
  37.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  38.   Else
  39.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  40.   End If
  41. End Function
  42. ' 获取指定硬盘的分区表类型(GPT/MBR)
  43. Function IsGPT(ByVal nDiskIndex)
  44.   IsGPT = "MBR"   'XP 不支持diskpart 识别GPT,默认MBR。感谢 freesoft00 提醒。
  45.   Dim wso, sLogFile, sText
  46.   Set wso = CreateObject("WScript.Shell")
  47.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  48.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  49.   Call TxtFile(sLogFile, 1, -2, sText)
  50.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  51. End Function
  52. ' 对文本指定编码进行读写操作2
  53. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  54. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  55.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  56.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  57.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  58.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  59.     If fso.GetFile(FileName).Size > 0 Then _
  60.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  61.   End If
  62. End Sub
  63. ' 以管理员身份运行
  64. Sub RunAsAdmin()
  65.   Dim objItems, objItem, strVer, nVer
  66.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  67.   For Each objItem In objItems
  68.     strVer = objItem.Version
  69.   Next
  70.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  71.   If nVer >= 60 Then
  72.     Dim oShell, oArg, strArgs
  73.     Set oShell = CreateObject("Shell.Application")
  74.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  75.       For Each oArg In WScript.Arguments
  76.         strArgs = strArgs & " """ & oArg & """"
  77.       Next
  78.       strArgs = strArgs & " /ADMIN:1"
  79.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  80.       Set oShell = Nothing
  81.       WScript.Quit(0)
  82.     End If
  83.     Set oShell = Nothing
  84.   End If
  85. End Sub
复制代码
1

评分人数

『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表