cmd.exe: complex conditions?

后端 未结 6 839
没有蜡笔的小新
没有蜡笔的小新 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 01:57

    No, there is no easier way.

    For and you can also just chain them without introducing blocks:

    if COND1 if COND2 ...
    

    which frankly isn't any worse than

    if COND1 and COND2 ...
    

    However, for or it gets uglier, indeed:

    set COND=
    if COND1 set COND=1
    if COND2 set COND=1
    if defined COND ...
    

    or:

    if COND1 goto :meh
    if COND2 goto :meh
    goto :meh2
    :meh
    ...
    :meh2
    

    I once saw a batch preprocessor which used C-like syntax for control flow and batch stuff in between and then converted such conditionals into multiple jumps and checks. However, that thing was for DOS batch files and was of little to no use in a Windows environment.

提交回复
热议问题