0

I made a cmd script to get the freespace of each drive in a PC and send an alert if under a value (the constant "MyLimit"). I found that the variables are not correctly evaluated in my "for" loop (the cycle within the list of drives)

This is the "for" loop:

Set MyLimit=107374182400
for /f "tokens=1-3" %%a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do (
    
    if not "%%c"=="" (

        echo FreeSpace=%%a
        echo Name=%%b
        echo Size=%%c
        
        if %%a LSS %MyLimit% (
          Echo not enough free space in %%b
        ) else (
            echo "do nothing"
        )    
    )
)

but the variable %%a always assumes the value of the last index of the "for" loop... then the IF always fails...

Where is the error?

Thanks for your support

Ale

I tried your suggestions, see below, but still it doesn't work maybe I put some error inside the code, I'm not so sure to have well done:

@Echo off
SETLOCAL enabledelayedexpansion

Set GB100=107374182400
Set TB_10=10995116277760

Set MyLimit=%TB_10%

for /f "tokens=1-3" %%a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do (
    
    if not "%%c"=="" (
    
        echo FreeSpace=%%a
        echo Name=%%b
        echo Size=%%c

        call :padNum %%a
        call :padNum MyLimit

        echo %%a
        echo %MyLimit%      

        if "%%a" LSS "%MyLimit%" (
          Echo not enough free space in %%b
        ) else (
            echo "nothing"
        )
    )
)


:padNum
setlocal enableDelayedExpansion
set "n=000000000000000!%~1!"
set "n=!n:~-15!"
endlocal & set "%~1=%n%"
exit /b

I intentionally put a high limit (10TB) to force the message...

Ale MaDaMa
  • 25
  • 4
  • 2
    Does this answer your question? [Windows batch file IF failure - How can 30000000000000 equal 40000000000?](https://stackoverflow.com/questions/9116365/windows-batch-file-if-failure-how-can-30000000000000-equal-40000000000) – Stephan Sep 09 '20 at 07:07
  • unfortunately not, I tried but it seems not working into a for loop... – Ale MaDaMa Sep 09 '20 at 08:00
  • Within a `for` loop, you'll probably need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). Please update your question with your new code (if it still doesn't work), so we can help you with it. – Stephan Sep 09 '20 at 08:03
  • The number `107374182400` is too big for a 32-bit signed number, so `if … lss …` will not provide the expected result… – aschipfl Sep 09 '20 at 09:31
  • I put the new code below, it still doesn't work... – Ale MaDaMa Sep 09 '20 at 09:31

1 Answers1

0

Just a little issue in your code: the metavariable %%a doesn't behave like normal %environmentVariables%, so you can't use them alike.

@Echo off
SETLOCAL enabledelayedexpansion

Set GB100=107374182400
Set TB_10=10995116277760
Set MyLimit=%TB_10%

for /f "tokens=1-3" %%a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do (
    
    if not "%%c"=="" (
    
        echo FreeSpace=%%a
        echo Name=%%b
        echo Size=%%c
        set free=%%a
        call :padNum free
        call :padNum MyLimit

        echo free=    !free!
        echo MyLimit= !MyLimit!      

        if "!free!" LSS "!MyLimit!" (
          Echo not enough free space in %%b
        ) else (
            echo "nothing"
        )
    )
)
goto :eof

:padNum
setlocal enableDelayedExpansion
set "n=000000000000000000!%~1!"
set "n=!n:~-18!"
endlocal & set "%~1=%n%"
exit /b

(and I took the freedom to expand from 15 to 18 chars, so you won't have trouble in the near future, when disks get bigger.)

Stephan
  • 53,940
  • 10
  • 58
  • 91