I can see that the issue has been resolved to everybody's satisfaction, but still have a side comment on this.
Compo wrote:TBH I see no real reason for accuracy when using integer math which rounds down
While it's true that the builtin integer division truncates (rounds down), it's also easy to use it to "round to nearest", instead.
Code: Select all
set/a truncated = a / b
set/a nearest = (a + b/2) / b
For low precision arithmetics, such as GBs and whole number percents, this is often enough to get integer results right. Below is a rewrite of the code in the original post...
Code: Select all
@echo off & setlocal
for /f "usebackq skip=1" %%X in (`
wmic logicaldisk where "mediatype='12'" get caption
`) do call :drive %%X
endlocal & goto :eof
:drive
if "%~1"=="" goto :eof
set "freeB=" & set "sizeB="
for /f "usebackq skip=1 tokens=1,2" %%X in (`
wmic logicaldisk where "DeviceID='%1'" get freespace^,size
`) do if not defined freeB (set "freeB=%%X" & set "sizeB=%%Y")
set/a freeMB = %freeB:~0,-6% & set/a sizeMB = %sizeB:~0,-6%
set/a freePCT = (100 * freeMB + sizeMB / 2) / sizeMB
call :mb2gib freeMB freeGiB & call :mb2gib sizeMB sizeGiB
echo Drive %1 %freeGiB% GB free / %sizeGiB% GB size = %freePCT%%% available (= %freeB% / %sizeB%)
goto :eof
:mb2gib
@rem double 1000/1024 mb->mib correction
set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128
@rem 1/1024 mib->gib conversion
set/a %2 = (%2 + 512) / 1024
...which gives accurate numbers in the majority of cases up to about 10 TB drives.
For 100% accuracy to the last digit and beyond, see
http://www.dostips.com/forum/viewtopic.php?p=20173#p20173. Squashman's link in the second post to the long division.bat still works, despite Judago's site being officially "abandoned" for a while now.
Liviu