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

[系统相关] [已解决]bat批处理,硬盘格式及分区检测,能否按照硬盘0、1、2、3的顺序依次显示?

[已解决]bat批处理,硬盘格式及分区检测,能否按照硬盘0、1、2、3的顺序依次显示?
  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
复制代码
1

评分人数

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

本帖最后由 yu2n 于 2015-1-21 06:27 编辑
  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 & vbTab
  19.         s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  20.         strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  21.         Set colPartitions = objWMI.ExecQuery _
  22.             ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  23.             & " where AssocClass=Win32_DiskDriveToDiskPartition")
  24.         For Each objPartition in colPartitions
  25.           strPartId = objPartition.DeviceID
  26.           Set colLogicalDisks = objWMI.ExecQuery _
  27.               ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
  28.               & """}  where AssocClass=Win32_LogicalDiskToPartition")
  29.           For Each objLogicalDisk in colLogicalDisks
  30.             size = objLogicalDisk.Size
  31.             free = objLogicalDisk.Freespace
  32.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  33.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  34.             s = s & "大小:" & GetSize(size) & vbTab
  35.             s = s & "剩余:" & GetSize(free) & vbTab
  36.             s = s & "使用率:" & used & vbCrLf
  37.           Next
  38.         Next
  39.         s = s & vbCrLf
  40.       End If
  41.     Next
  42.   Next
  43.   WScript.Echo s
  44. End Sub
复制代码
结果:
  1. 硬盘0:  大小:298.09GB   型号:ST3320613AS        分区表:MBR
  2. 分区C:  大小:100.07GB   剩余:92.77GB    使用率:7.30%
  3. 硬盘1:  大小:298.09GB   型号:Hitachi HDS721032CLA362    分区表:MBR
  4. 硬盘2:  大小:298.09GB   型号:Hitachi HDS721032CLA362    分区表:MBR
  5. 硬盘3:  大小:465.76GB   型号:WDC WD5000AAKX-001CA0      分区表:MBR
  6. 分区D:  大小:365.76GB   剩余:148.91GB   使用率:59.29%
复制代码
1

评分人数

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

TOP

结果:
yu2n 发表于 2015-1-21 06:24



    win7 64原版系统    2个硬盘环境   运行有错误。最后也没有显示啥结果。

能否在1楼代码基础上,简单修改下,实现?

TOP

本帖最后由 yu2n 于 2015-1-21 09:58 编辑

自行替换 45-76 行。

问之前能思考一下?
这样一点都不好,完全不懂的话要慢慢看,查清楚是做什么用的。
胡乱拼凑,害人害己。

坛子里面都看到好几个这样的了,不求甚解,一点都不去想代码会有怎样的后果。
甚至看到一个什么清理工具,随便删用户的数据文件,一个个分区的删,真心觉得这样不好。
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 ygqiang 于 2015-1-21 11:22 编辑

[已解决]多谢。。。。
  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.   ' 获取硬盘最大序号
  34.   nIndexMax = 0
  35.   For Each objDisk in colDisks
  36.     If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
  37.   Next
  38.   ' 按序号取信息
  39.   For i = 0 To nIndexMax
  40.     For Each objDisk in colDisks
  41.       ' 只取该序号信息
  42.       If objDisk.Index = i Then
  43.         s = s & "硬盘" & objDisk.Index & ":" & vbTab
  44.         s = s & "大小:" & GetSize(objDisk.Size) & vbTab
  45.         s = s & "型号:" & objDisk.Caption & vbTab
  46.         s = s & "分区表:" & IsGPT(objDisk.Index) & vbCrLf
  47.         strDiskID = Replace(objDisk.DeviceID, "\", "\\")
  48.         Set colPartitions = objWMI.ExecQuery _
  49.             ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDiskID & """}" _
  50.             & " where AssocClass=Win32_DiskDriveToDiskPartition")
  51.         For Each objPartition in colPartitions
  52.           strPartId = objPartition.DeviceID
  53.           Set colLogicalDisks = objWMI.ExecQuery _
  54.               ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & strPartId _
  55.               & """}  where AssocClass=Win32_LogicalDiskToPartition")
  56.           For Each objLogicalDisk in colLogicalDisks
  57.             size = objLogicalDisk.Size
  58.             free = objLogicalDisk.Freespace
  59.             used = FormatNumber((size - free) / size * 100, 2, true) & "%"
  60.             s = s & "分区" & objLogicalDisk.DeviceID & vbTab
  61.             s = s & "大小:" & GetSize(size) & vbTab
  62.             s = s & "剩余:" & GetSize(free) & vbTab
  63.             s = s & "使用率:" & used & vbCrLf
  64.           Next
  65.         Next
  66.         s = s & vbCrLf
  67.       End If
  68.     Next
  69.   Next
  70.   WScript.Echo s
  71. End Sub
  72. ' 格式化
  73. Function GetSize(intSize)
  74.   If intSize/1024/1024 > 1024 Then
  75.     GetSize = FormatNumber(intSize/1024/1024/1024, 2, true) & "GB"
  76.   Else
  77.     GetSize = FormatNumber(intSize/1024/1024, 2, true) & "MB"
  78.   End If
  79. End Function
  80. ' 获取指定硬盘的分区表类型(GPT/MBR)
  81. Function IsGPT(ByVal nDiskIndex)
  82.   IsGPT = "MBR"
  83.   Dim wso, sLogFile, sText
  84.   Set wso = CreateObject("WScript.Shell")
  85.   sLogFile = wso.ExpandenVironmentStrings("%temp%\diskpart.log")
  86.   wso.Run "cmd /c ""chcp 437 & cls & (echo list disk | diskpart | find /i ""Disk " & nDiskIndex & """) >""" & sLogFile & """ "" ", 0, False
  87.   Call TxtFile(sLogFile, 1, -2, sText)
  88.   If Trim(Right(sText,3)) = "*" Then IsGPT = "GPT"
  89. End Function
  90. ' 对文本指定编码进行读写操作2
  91. 'nRW: 1只读, 2只写, 8追加 'nCharset: -2(系统), -1(Unicode), 0(ASCII)
  92. Sub TxtFile(ByVal FileName, ByVal nRW, ByVal nCharset, ByRef sText)
  93.   Dim fso :  Set fso = CreateObject("Scripting.filesystemobject")
  94.   If sText <> "" And (nRW = 2 Or nRW = 8) Then
  95.     fso.OpenTextFile(FileName, nRW, True, nCharset).Write sText
  96.   ElseIf fso.FileExists(FileName) And nRW = 1 Then
  97.     If fso.GetFile(FileName).Size > 0 Then _
  98.       sText = fso.OpenTextFile(FileName, nRW, False, nCharset).ReadAll
  99.   End If
  100. End Sub
  101. ' 以管理员身份运行
  102. Sub RunAsAdmin()
  103.   Dim objItems, objItem, strVer, nVer
  104.   Set objItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  105.   For Each objItem In objItems
  106.     strVer = objItem.Version
  107.   Next
  108.   nVer = Split(strVer, ".")(0) & Split(strVer, ".")(1)
  109.   If nVer >= 60 Then
  110.     Dim oShell, oArg, strArgs
  111.     Set oShell = CreateObject("Shell.Application")
  112.     If Not WScript.Arguments.Named.Exists("ADMIN") Then
  113.       For Each oArg In WScript.Arguments
  114.         strArgs = strArgs & " """ & oArg & """"
  115.       Next
  116.       strArgs = strArgs & " /ADMIN:1"
  117.       Call oShell.ShellExecute("WScript.exe", """" & WScript.ScriptFullName & """" & strArgs, "", "runas", 1)
  118.       Set oShell = Nothing
  119.       WScript.Quit(0)
  120.     End If
  121.     Set oShell = Nothing
  122.   End If
  123. End Sub
复制代码

TOP

返回列表