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

[系统相关] [已解决]批处理如何判断硬盘是MBR分区还是GPT分区?

本帖最后由 pcl_test 于 2017-5-2 00:44 编辑

如题,批处理如何判断硬盘是MBR分区还是GPT分区?
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

我原来写的一段代码:
  1. :IsGPT
  2. rem 判定是否是GPT磁盘。
  3. echo list disk >yiwuyun.txt
  4. for /f "skip=8 tokens=3 delims=B" %%k in ('diskpart /s yiwuyun.txt') do (
  5. set temp=%%k
  6. if "!temp:~8,1!"=="*" goto gpt
  7. )
  8. set gpt=false
  9. echo Your disk is MBR disk!
  10. goto nogpt
  11. :gpt
  12. set gpt=true
  13. echo Your disk is GPT disk!
  14. :nogpt
  15. if exist yiwuyun.txt del yiwuyun.txt || exit /b 1
  16. rem echo errorlevel is !errorlevel!
  17. exit /b 0
复制代码

TOP

回复 2# yiwuyun
感谢提供一种方法,我改了下代码,表示可以判断

TOP

我原来写的一段代码:
yiwuyun 发表于 2014-12-31 19:14



    如果有2个或者多个硬盘,如何判断每个硬盘的情况呢?

TOP

如题,批处理如何判断硬盘是MBR分区还是GTP分区?
pcl_test 发表于 2014-12-31 13:00
  1. @echo off
  2. echo ——————————————————————————————————————— >nul 2>nul
  3. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  4. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  5.     more +%%i "%~f0" > c:\diskinfo.vbs
  6. )
  7. ping 127.0.0.1 -n 2 >nul 2>nul
  8. cls
  9. cscript /nologo c:\diskinfo.vbs
  10. ping 127.0.0.1 -n 2 >nul 2>nul
  11. CScript /NoLogo c:\diskinfo.vbs>c:\diskinfo.txt
  12. ping 127.0.0.1 -n 2 >nul 2>nul
  13. cls
  14. Type c:\diskinfo.txt
  15. ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  16. echo ——————————————————————————————————————— >nul 2>nul
  17. if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
  18. set "vbs=c:\diskinfo.vbs"
  19. set "log=c:\diskinfo.log"
  20. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (more +%%i "%~f0" > "%vbs%")
  21. CScript //NoLogo "%vbs%" > "%log%"
  22. cls
  23. type "%log%"
  24. ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul) & exit/b
  25. goto :eof
  26. echo ——————————————————————————————————————— >nul 2>nul
  27. :DynamicScript
  28. Main
  29. Sub Main()
  30.   Dim objWMI, colDisks
  31.   Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  32.   Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
  33.   For Each objDisk in colDisks
  34.     s = s & "硬盘" & objDisk.Index & ":" & vbTab
  35.     s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  36.     s = s & "型号:" & objDisk.Caption & vbTab
  37.     s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  38.     strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  39.     Set colPartitions = objWMI.ExecQuery _
  40.         ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  41.         & " where AssocClass=Win32_DiskDriveToDiskPartition")
  42.     For Each objPartition in colPartitions
  43.       strPartId = objPartition.DeviceID
  44.       Set colLogicalDisks = objWMI.ExecQuery _
  45.           ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId & """}  where AssocClass=Win32_LogicalDiskToPartition")
  46.       For Each objLogicalDisk in colLogicalDisks
  47.         size = objLogicalDisk.Size
  48.         free = objLogicalDisk.Freespace
  49.         used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  50.         s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  51.         s = s & "大小:" & GetSize(size) & vbTab
  52.         s = s & "剩余:" & GetSize(free) & vbTab
  53.         s = s & "使用率:" & used & vbCrLf
  54.       Next
  55.     Next
  56.     s = s & vbCrLf
  57.   Next
  58.   WScript.Echo s
  59. End Sub
  60. ' 格式化
  61. Function GetSize(intSize)
  62.   If intSize/1024/1024 > 1024 Then
  63.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  64.   Else
  65.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  66.   End If
  67. End Function
  68. ' 获取指定硬盘的分区表类型(GPT/MBR)
  69. Function IsGPT(ByVal nDiskIndex)
  70.   IsGPT = "MBR"
  71.   Dim wso, sLogFile, sText
  72.   Set wso = CreateObject("WScript.Shell")
  73.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  74.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  75.   Call TxtFile(sLogFile, 1, -2, sText)
  76.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  77. End Function
  78. ' 对文本指定编码进行读写操作2
  79. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  80. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  81.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  82.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  83.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  84.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  85.     If fso.GetFile(FileName).Size > 0 Then _
  86.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  87.   End If
  88. End Sub
  89. ' 以管理员身份运行
  90. Sub RunAsAdmin()
  91.   Dim objItems, objItem, strVer, nVer
  92.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  93.   For Each objItem In objItems
  94.     strVer = objItem.Version
  95.   Next
  96.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  97.   If nVer >= 60 Then
  98.     Dim oShell, oArg, strArgs
  99.     Set oShell = CreateObject("Shell.Application")
  100.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  101.       For Each oArg In WScript.Arguments
  102.         strArgs = strArgs & " """ & oArg & """"
  103.       Next
  104.       strArgs = strArgs & " /ADMIN:1"
  105.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  106.       Set oShell = Nothing
  107.       WScript.Quit(0)
  108.     End If
  109.     Set oShell = Nothing
  110.   End If
  111. End Sub
复制代码

TOP

回复 6# ygqiang
你好,我的电脑是GPT分区和MBR分区,但是检测的结果都是MBR分区\

TOP

回复 7# 迅雷会员
  1. echo;list disk|diskpart|findstr /rc:"磁盘 [0-9][0-9]*"
复制代码
显示什么

TOP

回复 8# pcl_test

\

TOP

回复 9# 迅雷会员


    6楼95行
  1. If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
复制代码
改为
  1. If InStrRev(sText, "*")>0 Then IsGPT = "GPT"
复制代码

TOP

回复 10# pcl_test

感谢版主回复,这下正常了

TOP

回复 6# ygqiang


    按照楼上版主的回复已经正常了

TOP

回复 5# ygqiang


    如果要把显示的内容保存在TXT文件应该如何操作呢

TOP

回复 12# 迅雷会员

6楼75行
  1. WScript.Echo s
复制代码
改为
  1. CreateObject("Scripting.filesystemobject").CreateTextFile("结果.txt",2).Write s
复制代码
1

评分人数

TOP

回复 9# pcl_test


請問一下,我的第二個 disk 是 GPT,但判斷出來依舊是 MBR

已經修改 If InStrRev(sText, "*")>0 Then IsGPT = "GPT"

TOP

本帖最后由 pcl_test 于 2017-5-1 09:58 编辑

回复 14# 飛雪
  1. If InStrRev(sText, "*", -1, 1)>0 Then IsGPT = "GPT"
复制代码

TOP

返回列表