我有一个名为Update.zip的文件,它的大小为50.25MB。 我也有这个命令:
for %%A in (Update) do set Size=%%~zA
echo %Size% MB
pause>nul
我得到这样的输出:50250。 我希望批处理文件以MB为单位输出文件大小,因此输出应如下所示:
50.25 MB
任何人
答案 0 :(得分:2)
Errare humanum est 。
为了更好地理解,这里有一个集成的脚本(确定版本,代码rem
注释中的解释);请注意:ConvToMi
子程序:
@ECHO OFF >NUL
SETLOCAL enableextensions
call :main
ENDLOCAL
goto :eof
:ConvToMi
:::::::::::::::::::::::::::::::::::::::
:::
::: %1 = a number to process, should be less than 2^31
::: %2 = (optional) conversion base (1024 or 1000); 1024 if omitted
:::
:::::::::::::::::::::::::::::::::::::::
SETLOCAL enableextensions disabledelayedexpansion
rem establish conversion base and units of measurement
set /A "kibi=10000%~2%%10000"
if %kibi% equ 0 set /A "kibi=1024"
set /A "mibi=kibi*kibi"
rem suffix
if %kibi% equ 1024 ( set "sfx=iB") else ( set "sfx=B ")
rem SizeB in bytes
set /A "SizeB=%~1" 2>NUL
if not %errorlevel% EQU 0 set "SizeB=%~1"
rem SizeK in kilobytes
set /a "SizeK=SizeB/kibi"
set /a "SizeKR=SizeB%%kibi"
rem reverse calculation for correctness check
set /a "BackK=kibi*SizeK+SizeKR"
if "%BackK%"=="%SizeB%" (set "failS= ") else (set "failS=f")
rem simple proportion from 1024ths to thousandths
set /a "SizeKR=(SizeKR*1000)/kibi"
rem three digits following decimal point
set "SizeKR=000%SizeKR%"
set "SizeKR=%SizeKR:~-3%"
set "SizeK3=%SizeK%.%SizeKR%"
rem SizeM in megabytes
set /a "SizeM=SizeB/mibi"
set /a "SizeMR=SizeB%%mibi"
rem simple proportion to millionths
rem divided to two sequenced steps as there is 2^31-1 limit
set /a "SizeMR=(SizeMR*1000)/kibi"
set /a "SizeMR=(SizeMR*1000)/kibi"
rem six digits following decimal point
set "SizeMR=000000%SizeMR%"
set "SizeMR=%SizeMR:~-6%"
set "SizeM6=%SizeM%.%SizeMR%"
rem round MB to 3 ciphers ("supermarket" round: 5 up)
set /A "SizeM3=((500+(1%SizeMR%%%1000)%%1000)/1000)+(1%SizeMR:~0,3%%%1000)"
rem treat a carry if any comes on
if %SizeM3% gtr 999 set /A "SizeM+=1"
rem three digits following decimal point
set "SizeM3=000%SizeM3%"
set "SizeM3=%SizeM3:~-3%"
set "SizeM3=%SizeM%.%SizeM3%"
echo %failS% %kibi%: %SizeB% B =%SizeK3% K%sfx% =%SizeM6% M%sfx% ~%SizeM3% M%sfx%
ENDLOCAL
goto :eof
:main
rem output verified for some values with calc.exe
set "testStr=13709312 1000 999999 999999999"
rem 2^27 neighbourhood
set "testStr=%testStr% 134217724 134217728 134217732"
rem semifinal value 2147483647==2^31-1 limit of 'set /A' command
rem final value 2147483648==2^31 exceeds limit, output defective
rem set /A "x=2147483648"
rem raises errorlevel=1073750992 with next message:
rem Invalid number. Numbers are limited to 32-bits of precision.
set "testStr=%testStr% 2147483647 2147483648"
rem set "testStr=2147483647 2147483648"
rem set "testStr=2147483647"
for %%A in ( %testStr% ) do (
call :ConvToMi %%A 1000
call :ConvToMi %%A
echo(
)
echo done
goto :eof
<强>输出强>:
==>28526273.bat
1000: 13709312 B =13709.312 KB =13.709312 MB ~13.709 MB
1024: 13709312 B =13388.000 KiB =13.074218 MiB ~13.074 MiB
1000: 1000 B =1.000 KB =0.001000 MB ~0.001 MB
1024: 1000 B =0.976 KiB =0.000953 MiB ~0.001 MiB
1000: 999999 B =999.999 KB =0.999999 MB ~1.000 MB
1024: 999999 B =976.561 KiB =0.953672 MiB ~0.954 MiB
1000: 999999999 B =999999.999 KB =999.999999 MB ~1000.000 MB
1024: 999999999 B =976562.499 KiB =953.674315 MiB ~953.674 MiB
1000: 134217724 B =134217.724 KB =134.217724 MB ~134.218 MB
1024: 134217724 B =131071.996 KiB =127.999996 MiB ~128.000 MiB
1000: 134217728 B =134217.728 KB =134.217728 MB ~134.218 MB
1024: 134217728 B =131072.000 KiB =128.000000 MiB ~128.000 MiB
1000: 134217732 B =134217.732 KB =134.217732 MB ~134.218 MB
1024: 134217732 B =131072.003 KiB =128.000002 MiB ~128.000 MiB
1000: 2147483647 B =2147483.647 KB =2147.483647 MB ~2147.484 MB
1024: 2147483647 B =2097151.999 KiB =2047.999999 MiB ~2048.000 MiB
f 1000: 2147483648 B =2147483.647 KB =2147.483647 MB ~2147.484 MB
f 1024: 2147483648 B =2097151.999 KiB =2047.999999 MiB ~2048.000 MiB
done
==>
但是,accepting属于npocmaka's answer ...
答案 1 :(得分:1)
为了更精确的计算,您可以使用1024
for %%A in (Update) do set SizeK=%%~zA
set /a SizeM=SizeK/1000
set /a SizeMR=SizeK%%1000
set Size=SizeM.SizeMR
echo %Size% MB
pause>nul