Ardit Hyka
Ardit Hyka

Reputation: 784

convert bytes to MB with a windows batch file

I have this short script

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

and it shows:

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

How to show bytes in MB
I know that i need to make this convertion byte/1024/1024 but how to do that in a batch script?

Upvotes: 2

Views: 4500

Answers (3)

Sunny
Sunny

Reputation: 8312

If your values are upto 32-bit integers, this works for you:

@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

Where input.txt is:

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

and you'll get output.txt as:

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

...and thus all values got converted from Bytes to MB.

Upvotes: 1

Sunny
Sunny

Reputation: 8312

Inspired from here, it works for you overcoming 2GB limitation of batch math in below short code:

@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)));

where input.txt is:

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

and your newout.txt would come as:

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

Upvotes: 2

Joey
Joey

Reputation: 354586

You can use set /a to do arithmetic:

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

You'll need to enable delayed expansion with

setlocal enabledelayedexpansion

first, though.

Upvotes: 1

Related Questions