-3

How to get free space on a storage media in GB or on more than 1024 GB in TB with 2 decimal places?

Below is my code:

:: Example batch file to show free disk space on C:

@echo off
cls
echo.
echo Free Space on C:
echo ========================================

for /f "tokens=1-3" %%n in ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:""') do set free=%%n& set total=%%p
echo.
rem echo %free% bytes free

set free=%free:~0,-3%
set /a free=%free%/1049
echo.
rem echo %free% MB free (approx and underestimated)

set /a free=%free%/1024
echo.
echo %free% GB free

pause > NUL
Mofi
  • 46,139
  • 17
  • 80
  • 143
rins
  • 7
  • 5
  • Please [edit](http://stackoverflow.com/posts/37321159/edit) your question and format your code sample correctly by using the *Code Sample* button... – aschipfl May 19 '16 at 13:24
  • Please do not multi-post the same question, you already asked for the same here: [folder size in gbwith 2 decimal points](http://stackoverflow.com/q/37292932)!! – aschipfl May 20 '16 at 08:12
  • 1
    Possible duplicate of [folder size in gbwith 2 decimal points](http://stackoverflow.com/questions/37292932/folder-size-in-gbwith-2-decimal-points) – aschipfl May 20 '16 at 08:12

1 Answers1

1

Since CMD variables can only work with numbers as integers, you'll have to do some tricks to manually place your decimal points. Something like this should work:

setlocal enabledelayedexpansion

for /f "tokens=1-3" %%n in ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:""') do set "freeB=%%n" & set "totalB=%%p"

set /a freeMB=%freeB:~0,-3%/1049
set /a freeGB=%freeB:~0,-3%/1049/1024

if %freeGB% geq 1000 (
    set /a freeTB=%freeGB%*1000/1024
    set freeTB=!freeTB:~0,-3!.!freeTB:~-3,-1!
)

if %freeMB% geq 1000 (
    set /a freeGB=%freeMB%*1000/1024
    set freeGB=!freeGB:~0,-3!.!freeGB:~-3,-1!
)

if defined freeTB echo %freeTB% TB Free 
if defined freeGB echo %freeGB% GB Free 
pause

So I kept playing with this just for fun. Bottom line: Batch files are really, really bad for doing math. CMD can't handle decimals, and can't handle any number over 2147483647, which makes working with numbers for Bytes and TeraBytes really cumbersome. That said, here's what I came up with. It still has limitations: It won't display sizes in the KB and MB range, but it handles GB and TB pretty accurately. I also started playing with inputs and outputs in functions, so there's that, too.

@echo off   

for /f "tokens=1-3" %%n in ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:""') do (
    set "freeB=%%n" 
    set "totalB=%%p"
)

rem use these to manually set your values for testing...
rem set freeB=581881856
rem set totalB=3000581881856

call :ConvertBytes %totalB% totalMB totalGB totalTB
call :ConvertBytes %freeB% freeMB freeGB freeTB

call :GetRightSizeAndInsertDecimal %totalGB% %totalMB% totalGBwDec
call :GetRightSizeAndInsertDecimal %totalTB% %totalGB% totalTBwDec
call :GetRightSizeAndInsertDecimal %freeGB% %freeMB% freeGBwDec
call :GetRightSizeAndInsertDecimal %freeTB% %freeGB% freeTBwDec

echo total space:
if defined totalTBwDec echo %totalB% Bytes (%totalTBwDec% TB)
if defined totalGBwDec echo %totalB% Bytes (%totalGBwDec% GB)
echo.
echo free space:
if defined freeTBwDec echo %freeB% Bytes (%freeTBwDec% TB)
if defined freeGBwDec echo %freeB% Bytes (%freeGBwDec% GB)
pause
goto :eof

:ConvertBytes   {bytes} {MB} {GB} {TB}
set bytes=%1
set /a %3=%Bytes:~0,-6%/1074
set /a %2=%Bytes:~0,-6%*1024/1074
set /a %4=%3/1024
exit /b

:GetRightSizeAndInsertDecimal   {check size} {stepdown size} {checksize w decimal}
if %1 lss 1000 if %1 gtr 0 (
    call :InsertDecimal %2 %3
)
exit /b

:InsertDecimal   {number} {number with decimal}
set /a newNum=%1*1000/1024
set "%2=%newNum:~0,-3%.%newNum:~-3,-1%" 
exit /b
Wes Larson
  • 1,042
  • 8
  • 17
  • i have one problem on this code disk space free size have 3.7tb but i readibg has 1.99 tb any solution for this – rins May 21 '16 at 15:04
  • @rins - click the check mark to the left of the question – SomethingDark May 23 '16 at 06:31
  • hi can u please tell why this code not reading 3.7 TB size from hard disk – rins May 23 '16 at 08:40
  • I don't have a TB disk to work with, but when I manually set `%freeB%` to look like a TB drive, it complains that the number is too large: `Invalid number. Numbers are limited to 32-bits of precision.` You may have to just cut off 6 digits to start... or else use PowerShell or VBScript to do the math. – Wes Larson May 23 '16 at 17:51