标题: [系统相关] [已解决]批处理如何判断硬盘是MBR分区还是GPT分区? [打印本页]
作者: pcl_test 时间: 2014-12-31 13:00 标题: [已解决]批处理如何判断硬盘是MBR分区还是GPT分区?
本帖最后由 pcl_test 于 2017-5-2 00:44 编辑
如题,批处理如何判断硬盘是MBR分区还是GPT分区?
作者: yiwuyun 时间: 2014-12-31 19:14
我原来写的一段代码:- :IsGPT
- rem 判定是否是GPT磁盘。
- echo list disk >yiwuyun.txt
- for /f "skip=8 tokens=3 delims=B" %%k in ('diskpart /s yiwuyun.txt') do (
- set temp=%%k
- if "!temp:~8,1!"=="*" goto gpt
- )
- set gpt=false
- echo Your disk is MBR disk!
- goto nogpt
- :gpt
- set gpt=true
- echo Your disk is GPT disk!
- :nogpt
- if exist yiwuyun.txt del yiwuyun.txt || exit /b 1
- rem echo errorlevel is !errorlevel!
- exit /b 0
复制代码
作者: pcl_test 时间: 2015-1-1 08:39
回复 2# yiwuyun
感谢提供一种方法,我改了下代码,表示可以判断
作者: ygqiang 时间: 2015-1-1 23:30
我原来写的一段代码:
yiwuyun 发表于 2014-12-31 19:14
如果有2个或者多个硬盘,如何判断每个硬盘的情况呢?
作者: ygqiang 时间: 2015-1-20 17:03
如题,批处理如何判断硬盘是MBR分区还是GTP分区?
pcl_test 发表于 2014-12-31 13:00
- @echo off
-
- echo ——————————————————————————————————————— >nul 2>nul
- if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
- more +%%i "%~f0" > c:\diskinfo.vbs
- )
-
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- cls
- cscript /nologo c:\diskinfo.vbs
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- CScript /NoLogo c:\diskinfo.vbs>c:\diskinfo.txt
- ping 127.0.0.1 -n 2 >nul 2>nul
-
- cls
- Type c:\diskinfo.txt
-
- ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
-
-
- echo ——————————————————————————————————————— >nul 2>nul
- if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul)
-
- set "vbs=c:\diskinfo.vbs"
- set "log=c:\diskinfo.log"
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (more +%%i "%~f0" > "%vbs%")
- CScript //NoLogo "%vbs%" > "%log%"
-
- cls
- type "%log%"
-
- ping 127.0.0.1 -n 5 >nul 2>nul & if exist "c:\diskinfo.*" (del /q "c:\diskinfo.*" >nul 2>nul) & exit/b
-
- goto :eof
-
-
-
- echo ——————————————————————————————————————— >nul 2>nul
- :DynamicScript
- Main
- Sub Main()
- 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 & "硬盘" & objDisk.Index & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & vbTab
- s = s & "分区表:" & IsGPT(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 & "分区" & objLogicalDisk.DeviceID & vbTab
- s = s & "大小:" & GetSize(size) & vbTab
- s = s & "剩余:" & GetSize(free) & vbTab
- s = s & "使用率:" & used & vbCrLf
- Next
- Next
- s = s & vbCrLf
- Next
- WScript.Echo s
- End Sub
-
- ' 格式化
- 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
-
- ' 获取指定硬盘的分区表类型(GPT/MBR)
- Function IsGPT(ByVal nDiskIndex)
- IsGPT = "MBR"
- Dim wso, sLogFile, sText
- Set wso = CreateObject("WScript.Shell")
- sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
- wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
- Call TxtFile(sLogFile, 1, -2, sText)
- If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
- End Function
-
- ' 对文本指定编码进行读写操作2
- 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
- Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
- Dim fso : Set fso = CreateObject("Scripting.filesystemobject")
- If sText <> "" And (nRW = 2 Or nRW = 8) Then
- fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
- ElseIf fso.FileExists(FileName) And nRW = 1 Then
- If fso.GetFile(FileName).Size > 0 Then _
- sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
- End If
- End Sub
-
- ' 以管理员身份运行
- Sub RunAsAdmin()
- Dim objItems, objItem, strVer, nVer
- Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
- For Each objItem In objItems
- strVer = objItem.Version
- Next
- nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
- If nVer >= 60 Then
- Dim oShell, oArg, strArgs
- Set oShell = CreateObject("Shell.Application")
- If Not WScript.Arguments.Named.Exists("ADMIN") Then
- For Each oArg In WScript.Arguments
- strArgs = strArgs & " """ & oArg & """"
- Next
- strArgs = strArgs & " /ADMIN:1"
- Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
- Set oShell = Nothing
- WScript.Quit(0)
- End If
- Set oShell = Nothing
- End If
- End Sub
复制代码
作者: 迅雷会员 时间: 2016-8-25 18:45
回复 6# ygqiang
你好,我的电脑是GPT分区和MBR分区,但是检测的结果都是MBR分区\
作者: pcl_test 时间: 2016-8-25 19:43
回复 7# 迅雷会员 - echo;list disk|diskpart|findstr /rc:"磁盘 [0-9][0-9]*"
复制代码
显示什么
作者: 迅雷会员 时间: 2016-8-26 08:14
回复 8# pcl_test
\
作者: pcl_test 时间: 2016-8-26 11:13
回复 9# 迅雷会员
6楼95行- If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
复制代码
改为- If InStrRev(sText, "*")>0 Then IsGPT = "GPT"
复制代码
作者: 迅雷会员 时间: 2016-8-26 12:46
回复 10# pcl_test
感谢版主回复,这下正常了
作者: 迅雷会员 时间: 2016-8-26 12:47
回复 6# ygqiang
按照楼上版主的回复已经正常了
作者: 迅雷会员 时间: 2016-8-26 18:22
回复 5# ygqiang
如果要把显示的内容保存在TXT文件应该如何操作呢
作者: pcl_test 时间: 2016-8-26 19:56
回复 12# 迅雷会员
6楼75行复制代码
改为- CreateObject("Scripting.filesystemobject").CreateTextFile("结果.txt",2).Write s
复制代码
作者: 飛雪 时间: 2017-5-1 09:33
回复 9# pcl_test
請問一下,我的第二個 disk 是 GPT,但判斷出來依舊是 MBR
已經修改 If InStrRev(sText, "*")>0 Then IsGPT = "GPT"
作者: pcl_test 时间: 2017-5-1 09:54
本帖最后由 pcl_test 于 2017-5-1 09:58 编辑
回复 14# 飛雪 - If InStrRev(sText, "*", -1, 1)>0 Then IsGPT = "GPT"
复制代码
作者: 飛雪 时间: 2017-5-1 10:16
回复 15# pcl_test
感謝版主回覆
好像只能判斷第一個 DISK 是 GPT,第二個 DISK 就會判斷成 MBR 了...
作者: pcl_test 时间: 2017-5-1 10:38
回复 16# 飛雪
6楼94、95行改为- Call TxtFile(sLogFile, 1, 0, sText)
- If InStrRev(sText, "*", -1, 1)>0 Then IsGPT = "GPT"
复制代码
如果还不行,把temp临时文件夹里的diskpart.log上传附件看看
作者: 飛雪 时间: 2017-5-1 10:42
回复 17# pcl_test
還是不行, %TMP% 底下的 log 只有一行
Disk 0 Online 60 GB 0 B
作者: 飛雪 时间: 2017-5-1 10:58
反覆運行後
我發現 disk 2 有時可以正常判斷為 GPT,但大多數都是判斷成 MBR
所以應該是 log 部分的寫入有問題
作者: pcl_test 时间: 2017-5-1 13:09
本帖最后由 pcl_test 于 2017-5-1 23:55 编辑
回复 19# 飛雪
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 & "硬盘" & objDisk.Index & ":" & vbTab
- s = s & "大小:" & GetSize(objDisk.Size) & vbTab
- s = s & "型号:" & objDisk.Caption & vbTab
- s = s & "分区表:" & 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 & "分区" & objLogicalDisk.DeviceID & vbTab
- s = s & "大小:" & GetSize(size) & vbTab
- s = s & "剩余:" & GetSize(free) & vbTab
- s = s & "使用率:" & 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
复制代码
作者: 飛雪 时间: 2017-5-1 15:19
回复 20# pcl_test
非常感謝,很完滿的正確顯示 MBR 跟 GPT 了
但是我使用批處理 cscript //Nologo 調用 vbs,變成彈窗了
有辦法直接在批處理中顯示嗎?
謝謝
作者: 飛雪 时间: 2017-5-1 17:35
已解決
將 Msgbox s mark 掉
下面加一行 WScript.Echo s 就可以了
謝謝版主
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |