Accurate Math in Batch

Home made / NONE
June 11, 2009 at 09:55:36
Specs: Windows XP, 2.8 ghz / 2 gig
I'm trying to convert file sizes from bytes into the largest unit with a whole number. So 1025 bytes i'd like to convert to kilobytes and 2197152 would be converted to megabytes.

I was well on my way with an if greater than 1023 divide by 1024, and then repeat that until it's less than 1024. But I found out set /a rounds to whole numbers.

Is there a simple way to get around this or a cmd line program to do accurate math?

Now that I say that I just learned how to use arguments with a VB program. I guess I could code up a simple VB program to do the math and return a value and unit. Is there a way to return a value back to the batch script or would i just have to use a temp file?

Any other ideas are greatly appreciated.

See More: Accurate Math in Batch

Report •

June 11, 2009 at 11:15:23
Well I got my VB program to convert any numeric input into b, kb, mb, or gb. But I can't get the batch file to load the result. I have the VB program make a txt file with the result.

login to ftp and create list~
%5 is the file size in bytes

if exist list~ (
  echo %date% %time% >> "upload list.txt"
  echo. >> "upload list.txt"
  for /f "usebackq tokens=*" %%a in (list~) do (
    call :write %%a
  echo. >> "upload list.txt"

del list~

  if not [%5] == [] (
    start /w convert.exe %5
    set /p size=<convert~
    del convert~
    echo %size% %6 %7 %8 %9 >> "upload list.txt"
    set size=
  goto :eof

I ran the following chunk by itself in another batch script and it echoed the result. So for some reason when it's with the rest of the script it doesn't load the file.

start /w convert.exe %5
set /p size=<convert~
del convert~
echo %size%

Report •

June 11, 2009 at 14:35:34
I have a huge batch file that can do that, I've posted it here rather that scrolling the post by ~500 lines. The binary components aren't finished yet but do work for unsigned numbers.

The numbers returned will be to ten decimal places, if needed, for division and decimal numbers can be input for operation. I'm sure you can figure out how to do your rounding based on the decimal.

The posting on pastebin will last for one month, please don't edit it unless your fixing something.

Report •

June 11, 2009 at 17:54:08
if you have Python on Windows
import sys
bytes = sys.argv[1]
bytes = float(bytes)
if bytes >= 1099511627776:
    terabytes = bytes / 1099511627776
    size = '%.10fT' % terabytes
elif bytes >= 1073741824:
    gigabytes = bytes / 1073741824
    size = '%.10fG' % gigabytes
elif bytes >= 1048576:
    megabytes = bytes / 1048576
    size = '%.10fM' % megabytes
elif bytes >= 1024:
    kilobytes = bytes / 1024
    size = '%.10fK' % kilobytes
    size = '%.10fb' % bytes
print size


C:\test>python 1024

C:\test>python 102444324242

here's an executable
c:\> convert.exe 123456788
or in batch
for /F %%a in ('convert.exe 12323131') do (
echo %%a

Report •

Related Solutions

Ask Question