I have used this question for getting disk size information. I am quite new to batch programming so please be patient here.
@echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
SET "diskfree=!disktotal!"
SET "disktotal=!diskavail!"
SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
(ECHO(Information for volume %volume%
ECHO(TOTAL SIZE ---------- %disktotal:~0,-9% GB
ECHO(AVAILABLE SIZE ------- %diskavail:~0,-9% GB)
pause
The result is this:
Information for volume C:
TOTAL SIZE ---------- 240 GB
AVAILABLE SIZE ------- 135 GB
How can I now calculate the used disk space by subtracting the available size from the total size? I know I have to use the SET command and /A but I do not know how to implement it right.
Replace this:
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
(ECHO(Information for volume %volume%
ECHO(TOTAL SIZE ---------- %disktotal:~0,-9% GB
ECHO(AVAILABLE SIZE ------- %diskavail:~0,-9% GB)
with this:
SET /a diskused=%disktotal:~0,-9% - %diskavail:~0,-9%
ECHO(Information for volume %volume%
ECHO(TOTAL SIZE ---------- %disktotal:~0,-9% GB
ECHO(AVAILABLE SIZE ------- %diskavail:~0,-9% GB
ECHO(USED SIZE ------------ %diskused% GB
Your original for no apparent reason makes a meal of assigning the values of disktotal and diskavail - it's redundant because they've already been assigned by the previous for.
The calculation is made using set /a, subtracting the available from the total, in GB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With