无论变量的值是什么,处理变量的特定数值的 If 语句总是落入最后一个 if 语句

问题描述 投票:0回答:1

我想根据PC的总RAM容量来处理不同的功能。

为此,我想出了这个函数:

: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

我的问题是,无论

%ram%
的值如何,它始终是最后一个被处理的 if 语句。即使我手动将
%ram%
的值设置为
8
,当我应该处理第一个 if 语句时,它仍然会处理最后一个 if 语句。我还将
%ram%
的值重定向到一个文本文件,看看是否有空格,但没有。

if-statement batch-file
1个回答
0
投票

您实际上并不需要设置变量,也不需要所有内部括号,也不需要

delayedexpansion

您可以简单地使用元变量

%%a
进行匹配。

:function.texture_streaming
for /f "delims=" %%a in ('PowerShell -Command "(Get-WmiObject Win32_PhysicalMemory | Measure Capacity -Sum).Sum/1GB"') do (
         if %%a lss 16 call :error.insufficient_ram %%a

         if %%a geq 16 if %%a lss 32 call :subroutine.limited_texture_streaming

         if %%a geq 32 call :subroutine.full_texture_streaming
         goto :prompt.performance
    )

此外,您不需要在每个

goto :prompt.performance
语句之后添加
if
,因为所有不匹配的 if 都将被忽略,并且所有这些都将到达最后一个
goto
 之后的 
if

© www.soinside.com 2019 - 2024. All rights reserved.