标题: [数值计算] [已解决]批处理如何以以MB和GB为单位转换内存和硬盘的容量大小? [打印本页]
作者: dennisle 时间: 2011-6-4 12:31 标题: [已解决]批处理如何以以MB和GB为单位转换内存和硬盘的容量大小?
本帖最后由 pcl_test 于 2016-7-17 20:11 编辑
請教批處理前輩及大哥們
小弟想求得實體內存計算成(MB)以及磁碟大小計算成(GB)- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=1* delims==" %%a in ('wmic path win32_physicalmemory get banklabel^,capacity /value 2^>nul') do (
- if /i "%%a"=="banklabel" set "banklabel=%%b"
- if /i "%%a"=="capacity" set /a "capacity=%%b/1024/1024" & echo.實體內存 = !capacity! MB !banklabel:~,-1!
- )
- for /f "tokens=1* delims==" %%a in ('wmic path win32_diskdrive get caption^,interfacetype^,size /value 2^>nul') do (
- if /i "%%a"=="caption" set "caption=%%b"
- if /i "%%a"=="size" set /a "size=%%b/1024/1024" & ECHO.磁碟名稱 = !Caption:~,-1! Size: !Disk! GB
- )
- pause >nul
复制代码
希望代碼能適用於XP及WIN7的環境,懇請不吝指教!!
作者: dennisle 时间: 2011-6-4 17:49
終於發現原來是被除數過大的限制!
已經解決問題了!!
作者: pcl_test 时间: 2016-7-17 20:53
bat+vbs- '&cls&cscript -nologo -e:vbscript "%~fs0"&pause&exit
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set wmi = GetObject("winmgmts:\\.\root\cimv2")
-
- WSH.echo GetMemory()&GetHardDisk()
-
- '内存容量
- Function GetMemory()
- Dim i, s
- Set Memory = wmi.execquery("select * from win32_physicalmemory")
- For Each item In Memory
- i = i+1
- s = s &"内存"& i &"[容量:"& GetSize(item.Capacity) &"]"& vbCrLf
- Next
- GetMemory = s
- End Function
-
- '硬盘容量
- Function GetHardDisk()
- Dim i, s
- Set HardDisk = wmi.execquery("select * from win32_diskdrive")
- For Each item In HardDisk
- i = i+1
- s = s &"硬盘"& i &"[名称:"& item.Caption &",接口类型:"& item.InterfaceType &",容量:"&GetSize(item.Size) &"]"& vbCrLf
- Next
- GetHardDisk = s
- End Function
-
- '转换单位
- Function GetSize(size)
- If size >= 1125899906842624 Then
- GetSize = FormatNumber(size/1125899906842624, 2, true) & "PB"
- ElseIf size >= 1099511627776 Then
- GetSize = FormatNumber(size/1099511627776, 2, true) & "TB"
- ElseIf size >= 1073741824 Then
- GetSize = FormatNumber(size/1073741824, 2, true) & "GB"
- ElseIf size >= 1048576 Then
- GetSize = FormatNumber(size/1048576, 2, true) & "MB"
- ElseIf size >= 1024 Then
- GetSize = FormatNumber(size/1024, 2, true) & "KB"
- Else
- GetSize = size & "字节"
- End If
- End Function
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |