标题: [系统相关] [已解决]bat批处理,硬盘格式及分区检测,能否按照硬盘0、1、2、3的顺序依次显示? [打印本页]
作者: ygqiang 时间: 2015-1-20 23:24 标题: [已解决]bat批处理,硬盘格式及分区检测,能否按照硬盘0、1、2、3的顺序依次显示?
[已解决]bat批处理,硬盘格式及分区检测,能否按照硬盘0、1、2、3的顺序依次显示?- @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
复制代码
作者: yu2n 时间: 2015-1-21 06:24
本帖最后由 yu2n 于 2015-1-21 06:27 编辑
- Main
- Sub Main()
- Dim objWMI, colDisks
- Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
- Set colDisks = objWMI.ExecQuery("select * from Win32_DiskDrive where MediaType like 'fixed%'")
- ' 获取硬盘最大序号
- nIndexMax = 0
- For Each objDisk in colDisks
- If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
- Next
- ' 按序号取信息
- For i = 0 To nIndexMax
- For Each objDisk in colDisks
- ' 只取该序号信息
- If objDisk.Index = i Then
- 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
- End If
- Next
- Next
-
- WScript.Echo s
- End Sub
复制代码
结果:- 硬盘0: 大小:298.09GB 型号:ST3320613AS 分区表:MBR
- 分区C: 大小:100.07GB 剩余:92.77GB 使用率:7.30%
-
- 硬盘1: 大小:298.09GB 型号:Hitachi HDS721032CLA362 分区表:MBR
-
- 硬盘2: 大小:298.09GB 型号:Hitachi HDS721032CLA362 分区表:MBR
-
- 硬盘3: 大小:465.76GB 型号:WDC WD5000AAKX-001CA0 分区表:MBR
- 分区D: 大小:365.76GB 剩余:148.91GB 使用率:59.29%
复制代码
作者: ygqiang 时间: 2015-1-21 07:10
结果:
yu2n 发表于 2015-1-21 06:24
win7 64原版系统 2个硬盘环境 运行有错误。最后也没有显示啥结果。
能否在1楼代码基础上,简单修改下,实现?
作者: yu2n 时间: 2015-1-21 09:47
本帖最后由 yu2n 于 2015-1-21 09:58 编辑
自行替换 45-76 行。
问之前能思考一下?
这样一点都不好,完全不懂的话要慢慢看,查清楚是做什么用的。
胡乱拼凑,害人害己。
坛子里面都看到好几个这样的了,不求甚解,一点都不去想代码会有怎样的后果。
甚至看到一个什么清理工具,随便删用户的数据文件,一个个分区的删,真心觉得这样不好。
作者: ygqiang 时间: 2015-1-21 11:21
本帖最后由 ygqiang 于 2015-1-21 11:22 编辑
[已解决]多谢。。。。- @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%'")
- ' 获取硬盘最大序号
- nIndexMax = 0
- For Each objDisk in colDisks
- If Cint(objDisk.Index) > nIndexMax Then nIndexMax = Cint(objDisk.Index)
- Next
- ' 按序号取信息
- For i = 0 To nIndexMax
- For Each objDisk in colDisks
- ' 只取该序号信息
- If objDisk.Index = i Then
- 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
- End If
- Next
- 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
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |