Question : Batch File: Convert bytes to either KB, MB or GB with 2 decimal points

Hi there,

I need to create a batch file that will convert a number in bytes to either KB, MB or GB with 2 decimal points.

@echo off

FOR %%A IN (4321,1342.56,2145386496) do (
   call :ConvertNumber
   ECHO "%%A" is easyer to read like this: %ConvertedNumber%
)
ECHO.
PAUSE
EXIT

:ConvertNumber
whatever script
exit /b

RESULT:
4.21 KB
1.99 GB


Hoping this will be easy points for you.

Cheers,
Rene

Answer : Batch File: Convert bytes to either KB, MB or GB with 2 decimal points

==> 1342.56

This is actually quite a bit harder than you might think in a BAT file.  Could something be worked up, yes (and as a Math major I like to play with things like this).  But math support in BAT is relatively weak, and only integer numbers are supported so any manipulation of decimals has to be performed manually in the BAT script.  In addition, numbers are limited to 2^31 in size, so they max out at 2147483647, and so as a result any number larger than 2GB can not be dealt with.

Some approximations can be done and that is what I have typically seen on EE, there are some questions that already have solutions to this, a little searching will likely turn these up.

I often use a couple of standalone EXE utilities that do math for any complex algebra in a BAT script.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24881845.html

~bp
Random Solutions  
 
programming4us programming4us