0

I want to check if the C: drive has at least a certain amount of storage left (in this case 60GB) and halt the execution if it doesn't. But I am stuck with CMD telling me that 160000 is less than 60000 and I can't figure out why.

This is the code:

@echo off & setlocal enableextensions enabledelayedexpansion

:: If disk space is less than this value, script will not be executed
SET DISK_SPACE_ERROR_AMOUNT_MB=60000

FOR /f "tokens=3" %%i IN ('dir /-c^|findstr /c:"bytes free"') DO SET "Free=%%i"
:: Free contains the bytes of storage left

SET /A DiskFreeKB=%Free:~,-3%
SET /A DiskFreeMB=%DiskFreeKB%/1024

echo Disk Space Check:
echo %DiskFreeKB% KB
echo %DiskFreeMB% MB

echo Err amount %DISK_SPACE_ERROR_AMOUNT_MB%

IF "%DiskFreeMB%" LSS %DISK_SPACE_ERROR_AMOUNT_MB% (
    :: DiskFreeMB less than error amount
    echo.
    echo ERROR
    echo Not enough space left - %DiskFreeMB% MB of the required minimum of %DISK_SPACE_ERROR_AMOUNT_MB% MB
    echo ERROR
    echo.
    
    PAUSE
    EXIT 1
) ELSE (
    echo Disk space is ok
    :: (Continue with other code)
)

This is the console's output:
image

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Sv443
  • 708
  • 1
  • 7
  • 27

2 Answers2

2

I would suggest you use for this task:

@"%__AppDir__%wbem\WMIC.exe" Volume Where "DriveLetter='%SystemDrive%' And FreeSpace >='60000000000'" Get FreeSpace 2> NUL | "%__AppDir__%findstr.exe" "[0-9]" 1> NUL || Exit 1
@Rem Your code here
@Echo Enough Space & Pause

You could beautify that if you like

@"%__AppDir__%wbem\WMIC.exe" Volume Where ^
 "DriveLetter='%SystemDrive%' And FreeSpace >='60000000000'" ^
 Get FreeSpace 2> NUL | "%__AppDir__%findstr.exe" "[0-9]" 1> NUL || Exit 1
@Rem Your code here
@Echo Enough Space & Pause

In both examples, the last two lines are for informational purposes.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

I recommend using the difference instead.

Subtract the Free space from the minimum required amount, and if it is <= 0, then you don't have enough space.

SET /A delta=%DiskFreeMB% - %DISK_SPACE_ERROR_AMOUNT_MB%
if %delta% LEQ 0 ( echo Out of Space )

Here is a short little test program

@echo off
echo Test1:  Sufficient Disk Space
set DiskFreeMB=600
set DISK_SPACE_ERROR_AMOUNT_MB=500
SET /A ErrorDelta=%DiskFreeMB% - %DISK_SPACE_ERROR_AMOUNT_MB%
if %ErrorDelta% LEQ 0 ( echo Out of Space )
echo.

echo Test2: Not enough space.
set DiskFreeMB=400
set DISK_SPACE_ERROR_AMOUNT_MB=500
SET /A ErrorDelta=%DiskFreeMB% - %DISK_SPACE_ERROR_AMOUNT_MB%
if %ErrorDelta% LEQ 0 ( echo Out of Space )
echo.

echo Test2: Exactly Full
set DiskFreeMB=500
set DISK_SPACE_ERROR_AMOUNT_MB=500
SET /A ErrorDelta=%DiskFreeMB% - %DISK_SPACE_ERROR_AMOUNT_MB%
if %ErrorDelta% LEQ 0 ( echo Out of Space )
echo.

Output:

Test1:  Sufficient Disk Space

Test2: Not enough space.
Out of Space

Test2: Exactly Full
Out of Space
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • I'm now getting the error `The syntax of the command is incorrect.`. My modified IF looks like this: `SET /A ErrorDelta=%DiskFreeMB% - %DISK_SPACE_ERROR_AMOUNT_MB% IF %ErrorDelta% LEQ 0 ( echo Out of Space )` – Sv443 Jun 30 '20 at 14:49