Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
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.

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 :eofI 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

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.

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 sizeoutput
C:\test>python test.py 1024 1.0000000000K C:\test>python test.py 102444324242 95.4087118078Ghere's an executable
usage:
c:\> convert.exe 123456788
or in batch
for /F %%a in ('convert.exe 12323131') do (
echo %%a
)

![]() |
![]() |
![]() |

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