cmd.exe: complex conditions?

后端 未结 6 841
没有蜡笔的小新
没有蜡笔的小新 2021-01-08 01:25

in DOS batch files, In an IF statement, is it possible to combine two or more conditions using AND or OR ? I was not able to find any documentation for that

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-08 02:04

    If your conditions are checking for specific values of a variable, you might be able to set it up like a CASE statement. I had a need to check if the TYPE was an individual member or a group (or all).

    Note: labels are case insensitive.

    If any of the conditions are true (if TYPE==ALL or TYPE==GROUP1 or TYPE==GROUP2), then the code block executes, otherwise execution skips over the block.

    goto :TYPE_EQ_%TYPE% 2>NUL
    if %ERRORLEVEL% neq 0 goto :END_CASE_TYPE
    :TYPE_EQ_ALL
    :TYPE_EQ_GROUP1
    :TYPE_EQ_GROUP2
    rem do what you need
       echo GROUP: %TYPE%
    :END_CASE_TYPE
    

    I don't like Mehrdad's Electrical Engineering (digital electronics) approach. While accurate, with software engineering, the idea, as indicated by the OP, is to make things simpler to figure out. Nested if statements add Complexity (McCabe). Adding extra NOTs doesn't help.

    My solution is a little odd if you don't know the goto-label/error set-up, but it's a flat structure that is roughly set up like an if-statement with several OR'd conditions that are not to hard to decipher.

提交回复
热议问题