Using multiple IF statements in a batch file

后端 未结 5 2005
情深已故
情深已故 2021-02-01 07:28

When using more than 1 IF statement, is there a special guideline that should be followed? Should they be grouped? Should I use parenthesis to wrap the command(s)?

An ex

5条回答
  •  执念已碎
    2021-02-01 07:37

    IF EXIST "somefile.txt" (
      IF EXIST "someotherfile.txt" (
        SET var="somefile.txt","someotherfile.txt"
      )
    ) ELSE (
      CALL :SUB
    )
    :SUB
    ECHO Sorry... nothin' there.
    GOTO:EOF
    

    Is this feasible?

    SETLOCAL ENABLEDELAYEDEXPANSION
    IF EXIST "somefile.txt" (
      SET var="somefile.txt"
      IF EXIST "someotherfile.txt" (
        SET var=!var!,"someotherfile.txt"
      )
    ) ELSE (
      IF EXIST "someotherfile.txt" (
        SET var="someotherfile.txt"
      ) ELSE (
      GOTO:EOF
      )
    )
    

提交回复
热议问题