Board logo

标题: [系统相关] [已解决]批处理怎样获取C盘所在硬盘的分区类型? [打印本页]

作者: 小白龙    时间: 2023-8-31 08:14     标题: [已解决]批处理怎样获取C盘所在硬盘的分区类型?

本帖最后由 小白龙 于 2023-8-31 09:26 编辑

我想获取C盘所在硬盘的分区类型, 下面文章有用diskpart和powershell的命令
https://www.51cto.com/article/655573.html?pc

但我感觉wmic命令更通用一些, 而且在win7下并不能使用powershell那个命令, 使用diskpart似乎也麻烦

我尝试命令 wmic diskdrive 没有找到分区类型相关的信息
作者: 小白龙    时间: 2023-8-31 08:26

下面是一言的回答, 报错
  1. @echo off  
  2. set disk=  
  3. set /p disk=请输入磁盘的设备ID(例如:0):  
  4. set partitions=  
  5. for /f "tokens=1" %%a in ('wmic diskdrive where deviceid="%disk%" get deviceid') do (  
  6.     set partitions=%%a  
  7. )  
  8. if defined partitions (  
  9.     for /f "tokens=1" %%a in ('wmic partition where deviceid="%partitions%" get deviceid') do (  
  10.         set volumes=%%a  
  11.     )  
  12.     for /f "tokens=2 delims=," %%b in ('vol %volumes%') do (  
  13.         set drive_type=%%b  
  14.     )  
  15.     if /i "%drive_type%"=="MBR" (  
  16.         echo The partition form of the disk is: MBR  
  17.     ) else (  
  18.         echo The partition form of the disk is: GPT  
  19.     )  
  20. ) else (  
  21.     echo Cannot find the specified disk.  
  22. )
  23. pause
复制代码

作者: Batcher    时间: 2023-8-31 09:15

回复 2# 小白龙


test-1.bat
  1. @echo off
  2. for /f "delims=" %%i in ('wmic Partition where "BootPartition='true'" get type /value ^| find "="') do (
  3.     echo,%%i | find "GPT" >nul
  4.     if errorlevel 1 (
  5.         echo MBR
  6.     ) else (
  7.         echo GPT
  8.     )
  9. )
  10. pause
复制代码

作者: 小白龙    时间: 2023-8-31 09:26

回复 3# Batcher


    多谢大佬,

如果不是C盘, 是任一盘符呢? 可能是在磁盘3或磁盘5
作者: Batcher    时间: 2023-8-31 09:33

回复 4# 小白龙
  1. wmic Partition get * /value
复制代码
观察一下这个命令的结果,看看用哪个条件比较合适,然后修改 where 后面的条件。

如果需要模糊匹配,请参考这个 like 语法:
http://bbs.bathome.net/thread-67172-1-1.html#pid273113
作者: 小白龙    时间: 2023-8-31 09:36

回复 5# Batcher


    没有找到盘符信息
作者: 小白龙    时间: 2023-8-31 14:49

本帖最后由 小白龙 于 2023-8-31 14:50 编辑

回复 5# Batcher

下面这个命令的输出有盘符了, 但是没有类型了, 完美错过, 哎
    wmic logicaldisk get * /value

难道用wmic命令对于我的问题无解吗?
问题是: 判断某个特定的盘符的所在的分区的类型
作者: Five66    时间: 2023-8-31 15:47

额,之前不是解决过了???,用assoc关联特定类或输出特定关联类方式
就是下面帖子链接
http://www.bathome.net/thread-67149-1-2.html
第12楼那个,将where后面的"volumename='soft'"改成"name='盘符'"就是了
作者: Batcher    时间: 2023-8-31 17:17

回复 4# 小白龙


test-1.bat
  1. @echo off
  2. REM 设置待查看的盘符
  3. set "MyLetter=C:"
  4. for /f "tokens=1-2 delims==" %%a in ('wmic LogicalDisk where "DeviceID='%MyLetter%'" assoc:value /resultclass:Win32_DiskPartition ^| findstr /b /c:"DiskIndex=" /c:"Index="') do (
  5.     for /f "delims=" %%i in ("%%b") do (
  6.         if "%%a" equ "DiskIndex" (
  7.             set "LD2DI=%%i"
  8.         ) else if "%%a" equ "Index" (
  9.             set "LD2I=%%i"
  10.         )
  11.     )
  12. )
  13. for /f "delims=" %%i in ('wmic Partition where "DiskIndex='%LD2DI%' and Index='%LD2I%'" get type /value ^| find "="') do (
  14.     echo,%%i | find "GPT" >nul
  15.     if errorlevel 1 (
  16.         echo MBR
  17.     ) else (
  18.         echo GPT
  19.     )
  20. )
  21. pause
复制代码
test-2.bat
  1. @echo off
  2. REM 设置待查看的盘符
  3. set "MyLetter=C:"
  4. for /f "delims=" %%i in ('wmic LogicalDisk where "DeviceID='%MyLetter%'" assoc:value /resultclass:Win32_DiskPartition ^| findstr /b "Type="') do (
  5.     echo,%%i | find "GPT" >nul
  6.     if errorlevel 1 (
  7.         echo MBR
  8.     ) else (
  9.         echo GPT
  10.     )
  11. )
  12. pause
复制代码

作者: 小白龙    时间: 2023-8-31 18:06

回复 8# Five66


    怎么改, 改了不行,
作者: 小白龙    时间: 2023-8-31 18:07

回复 9# Batcher


  多谢大佬,
本想着一行命令就好了, 感觉复杂多了, 一时不好消化
作者: Five66    时间: 2023-8-31 18:11

回复 10# 小白龙


    Name=后面那个就是盘符,不行就换成DeviceID=
  1. wmic logicaldisk where "Name='C:'" assoc:value /RESULTCLASS:Win32_DiskPartition |findstr /i "Type="
复制代码

作者: 小白龙    时间: 2023-8-31 18:11

回复 9# Batcher

感觉是不是可以不用判断硬盘的id了, 因为分区的类型前面如果有gpt, 整个硬盘就是gpt, 总感觉有可能一行代码搞定
作者: 小白龙    时间: 2023-8-31 18:13

回复 12# Five66


    还真是, 一行代码就搞定了, 感谢!
作者: 小白龙    时间: 2023-8-31 18:23

回复 12# Five66

输出后是  Type=GPT: Basic Data  怎样只输出 GPT: Basic Data 呢, 我尝试在前面加 /value 但是报错
作者: Five66    时间: 2023-8-31 18:43

回复 15# 小白龙


    其实有gpt+mbr混合分区的,这样就可能不准确了, 想得到GPT: Basic Data就得用cmd for语句切分或set语句后截取字符串
作者: 小白龙    时间: 2023-8-31 18:59

回复 16# Five66


    gpt+mbr混合分区, 只在老苹果下有用到, 基本上很少了, 现在都是GPT了




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2