cmd.exe: complex conditions?

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

    For the AND workaround I agree with Joey, I cannot see another solution.

    For the OR workaround I would propose the following 'trick':
    Lets say that you want to test if user asked for help on the command line while calling for your batch file, something like: thebatch /?.
    But you don't know if the user will type /? or rather (being a Unix accustomed user) --help or -h or even forget to pass any argument at all.

    Your code could be like:

    for %%P in ("" "-h" "--help") do if "%1"==%%P (
       :help
       echo this is my help...  >&2
       exit /b 9
    )
    

    Note however that testing "/?" or "-?" strings won't work with that trick (FOR loops don't like the ? meta character).
    For that purpose, just add the line: if "%1"=="/?" goto help on top of the block (see the ':help' label inside the for loop ?)

    Note too that this syntax won't work (it is just completely ignored and code won't be executed) with batch files under TCC.EXE (the Take Command interpreter, either full or lite edition) but in that case just use their syntax with their .OR. and .AND. keywords.

    Hope this helps.

提交回复
热议问题