请注意,批处理仅适用于整数。您的一些计算会产生 0 MB 的值。这是一个关于如何使用十进制值的粗略示例。
@echo off
call :Parse > Newoutput.txt
exit /b 0
:Parse
for /f "tokens=1,2" %%A in (Output.txt) do call :ToMB "%%~B" "%%~A" || echo(%%A %%B
exit /b 0
:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
exit /b 0
:ToMB <String> <Name>
setlocal
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number= %Number%"
set "Name=%~2 "
echo %Name:~0,12%%Number:~-3%.%Decimal:~-3%
endlocal
exit /b 0
- 更新:在输出中添加了 AppName 以及一些格式。(多于)
- 更新:添加了 Newoutput.txt 重定向示例。(多于)
- 更新:添加了对所有标记的转换支持并改进了格式。(以下)
- 更新:为 find 命令添加了第一行跳过修复。(以下)
@echo off
call :Parse > Newoutput.txt
exit /b 0
:Parse
setlocal
for /f "tokens=1,* delims=]" %%A in ('type "Output.txt" ^|find /n /v ""') do (
for /f "tokens=1,2" %%X in ("%%~B") do call :Convert "%%~X" "%%~Y"
call :Blank "%%~B"
)
endlocal
exit /b 0
:Blank <String>
set "String=%~1"
if not defined String echo.
exit /b 0
:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
if "%~1"=="" exit /b 2
exit /b 0
:Convert <String> <String>
call :Calculate "%~1" Y || call :Display "%~1" Y
call :Calculate "%~2" || call :Display "%~2"
echo.
exit /b 0
:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number=000%Number%"
call :Display "%Number:~-3%.%Decimal:~-3%" %2
exit /b 0
:Display <String> [Pad]
set "String=%~1"
set "Pad=%~2"
if defined Pad set "String=%String% "
if defined String set /p "=%String:~0,24%" <nul
exit /b 0
- 更新:将 PowerShell 添加到计算例程以处理高达 2^64 的值(下)
:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number="
set "Decimal="
for /f "tokens=1,2 delims=." %%A in ('"PowerShell %~1 / ( 1024 * 1024 )"') do (
set "Number=%%A"
set "Decimal=%%B000"
)
call :Display "%Number%.%Decimal:~0,3%" %2
exit /b 0