Computing.Net > Forums > Programming > Accurate Math in Batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Accurate Math in Batch

Reply to Message Icon

Name: pball
Date: June 11, 2009 at 09:55:36 Pacific
OS: Windows XP
CPU/Ram: 2.8 ghz / 2 gig
Product: Home made / NONE
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: pball
Date: June 11, 2009 at 11:15:23 Pacific
Reply:

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~

:write
  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%
pause


0

Response Number 2
Name: Judago
Date: June 11, 2009 at 14:35:34 Pacific
Reply:

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.


0

Response Number 3
Name: ghostdog
Date: June 11, 2009 at 17:54:08 Pacific
Reply:

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
else:
    size = '%.10fb' % bytes
print size

output

C:\test>python test.py 1024
1.0000000000K

C:\test>python test.py 102444324242
95.4087118078G

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Accurate Math in Batch

math in batch www.computing.net/answers/programming/math-in-batch/16890.html

Numbers and basic math in BATCH www.computing.net/answers/programming/numbers-and-basic-math-in-batch/20285.html

Math in batch files www.computing.net/answers/programming/math-in-batch-files/14219.html