0

I am trying to get the freespace on a hard drive, and then compare it to a value, (to make sure we are not running out of disk space).

I can get it all to work except the final comparison. I am assuming it is because of the format that the free space value is being stored in, but I am not sure how to adjust my code.

My Code:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='W:'" get FreeSpace /format:value`) do set FreeSpace=%%x

If %FreeSpace% LEQ 1000000000 (
    echo %FreeSpace% is Less Than 1G of free space
    pause
) else (
    echo %FreeSpace% is Greater than 1G of free space
    pause
)

I want to know if the freespace is greater or less than 1GB. However, it always tells me that it is lower, no matter what value I use for comparison.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Try it like this `for /f "tokens=2 delims=," %%g in ('%__APPDIR__%wbem\WMIC.exe logicaldisk where "DeviceID='W:'" get FreeSpace /format:csv') do set "FreeSpace=%%g" If %FreeSpace% LEQ 1000000000 ( echo %FreeSpace% is Less Than 1G of free space & pause ) else ( echo %FreeSpace% is Greater than 1G of free space & pause)` – Qwerty Jan 17 '23 at 20:09
  • WMIC uses a different character encoding, so you need to be careful about capturing the result as a variable. For example: [WMIC Encoding](https://stackoverflow.com/questions/55310573/wmic-command-in-batch-outputting-non-utf-8-text-files) – Qwerty Jan 17 '23 at 20:12
  • You can also perform the comparison directly using `WHERE "FreeSpace < 1073741824"` Or of course the opposite, depending upon your needs, `WHERE "FreeSpace > 1073741824"`. – Compo Jan 17 '23 at 20:17
  • Qwerty, Thank you for your assistance. I have finally gotten it to work. @("%__AppDir__%wbem\WMIC.exe" LogicalDisk Where "DeviceID='W:' And FreeSpace>'10000000000000'" Get FreeSpace /Value 2>NUL|"%__AppDir__%find.exe" "=">NUL&&(Goto LessThan)||(Goto GreaterThan)) – Jeff Moore Jan 17 '23 at 20:48
  • Please be aware @JeffMoore, that WMIC is deprecated and is reportedly soon to be removed from future Windows versions. It would be better therefore to change the methodology, and/or scripting language, moving forward. – Compo Jan 17 '23 at 22:00
  • If WMIC was not an option, you could get this information from other sources, like fsutil: `%__APPDIR__%fsutil.exe volume diskfree W: | %__APPDIR__%findstr.exe /R /C:"Total free bytes"` – Qwerty Jan 18 '23 at 13:34

0 Answers0