使用Windowsbatch file将字节转换为MB

我有这个简短的脚本

@echo off for %%i in (*.msu) do ( echo %%i - %%~zi ) 

并显示:

 Windows8.1-KB2919355-x64.msu - 724339463 Windows8.1-KB2932046-x64.msu - 50327684 Windows8.1-KB2934018-x64.msu - 132577686 Windows8.1-KB2937592-x64.msu - 309819 Windows8.1-KB2938439-x64.msu - 20533883 

如何以MB为单位显示字节
我知道我需要做这个转换字节/ 1024/1024,但如何在批处理脚本中做到这一点?

受到这里的启发 , 它可以帮助您克服以下短代码中批量数学的2GB限制:

 @if (@CodeSection == @Batch) @then @echo off setlocal enabledelayedexpansion set JScall=Cscript //nologo //E:JScript "%~F0" for /f "tokens=1,3 delims= " %%i in (input.txt) do ( for /f %%a in ('%JScall% "%%j/1024/1024"') do set a=%%a for /f "delims=." %%z in ("!a!") do set a=%%z echo %%i !a!>>newout.txt ) ) goto :EOF @end WScript.Echo(eval(WScript.Arguments.Unnamed.Item(0))); 

其中input.txt是:

 Windows8.1-KB2919355-x64.msu - 724339463 Windows8.1-KB2932046-x64.msu - 50327684 Windows8.1-KB2934018-x64.msu - 132577686 Windows8.1-KB2937592-x64.msu - 309819 Windows8.1-KB2938439-x64.msu - 20533883 Windows8.1-KB2938439-x64.msu - 3724339463 

和你的newout.txt会来:

 Windows8.1-KB2919355-x64.msu 690 Windows8.1-KB2932046-x64.msu 47 Windows8.1-KB2934018-x64.msu 126 Windows8.1-KB2937592-x64.msu 0 Windows8.1-KB2938439-x64.msu 19 Windows8.1-KB2938439-x64.msu 3551 

你可以使用set /a来做算术运算:

 set /a size=%%~zi / 1024 / 1024 echo %%i - !size! 

您需要启用延迟扩展

 setlocal enabledelayedexpansion 

首先,虽然。

如果您的值高达32位整数,这适用于你:

 @echo off call :Parse > output.txt exit /b 0 :Parse for /f "tokens=1,3 delims= " %%A in (input.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,30%%Number:~-3%.%Decimal:~-3% endlocal exit /b 0 

其中input.txt是:

 Windows8.1-KB2919355-x64.msu - 724339463 Windows8.1-KB2932046-x64.msu - 50327684 Windows8.1-KB2934018-x64.msu - 132577686 Windows8.1-KB2937592-x64.msu - 309819 Windows8.1-KB2938439-x64.msu - 20533883 

你会得到output.txt为:

 Windows8.1-KB2919355-x64.msu 690.783 Windows8.1-KB2932046-x64.msu 47.996 Windows8.1-KB2934018-x64.msu 126.435 Windows8.1-KB2937592-x64.msu 0.294 Windows8.1-KB2938439-x64.msu 19.582 

…因此所有的值都从字节转换为MB。