if-statementbatch-file

If statements to process a specific numeric value of a variable always fall into the last if statement no matter the value of the variable


I want to process different functions depending on the total RAM capacity of a PC.

To do this I came up with that function:

:function.texture_streaming
setlocal EnableDelayedExpansion
for /f "usebackq tokens=*" %%a in (
    `PowerShell -Command "(Get-WmiObject Win32_PhysicalMemory | Measure Capacity -Sum).Sum/1GB"`
) do (
    set "ram=%%a"
)
if "%ram%" lss "16" (
    call :error.insufficient_ram "!ram!"
    endlocal
    goto :prompt.performance
)
if "%ram%" geq "16" if "%ram%" lss "32" (
    call :subroutine.limited_texture_streaming
    endlocal
    goto :prompt.performance
)
if "%ram%" geq "32" (
    call :subroutine.full_texture_streaming
    endlocal
    goto :prompt.performance
    )
endlocal
goto :prompt.performance

My issue is that no matter the value of %ram% it's always the last if statement that gets processed. Even if I set the value of %ram% to 8 manually it still processes the last if statement when I it should process the first one. I have also redirected the value of %ram% to a text file to see if there are any spaces but there are none.


Solution

  • Thanks to @Mofi and @Compo I was able to solve the issue.

    This is the now working code:

    setlocal
    rem Initialize variable.
    set "ram="
    
    for /f "usebackq tokens=*" %%a in (
        `PowerShell -NoLogo -NoProfile -Command "(Get-CIMInstance CIM_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB"`
    ) do (
        set "ram=%%a"
    )
    
    if not defined ram (
        call :error.function_failed "determining your total RAM capacity"
        endlocal
        goto :prompt.performance
    )
    
    if %ram% lss 16 (
        pause
        call :error.insufficient_ram "%ram%"
        endlocal
        goto :prompt.performance
    )
    
    if %ram% geq 16 if %ram% lss 32 (
        call :subroutine.limited_texture_streaming
        endlocal
        goto :prompt.performance
    )
    
    if  %ram% geq 32 (
        call :subroutine.full_texture_streaming
        endlocal
        goto :prompt.performance
    )
    
    endlocal
    goto :prompt.performance