Using multiple IF statements in a batch file

后端 未结 5 2007
情深已故
情深已故 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:39

    is there a special guideline that should be followed

    There is no "standard" way to do batch files, because the vast majority of their authors and maintainers either don't understand programming concepts, or they think they don't apply to batch files.

    But I am a programmer. I'm used to compiling, and I'm used to debuggers. Batch files aren't compiled, and you can't run them through a debugger, so they make me nervous. I suggest you be extra strict on what you write, so you can be very sure it will do what you think it does.

    There are some coding standards that say: If you write an if statement, you must use braces, even if you don't have an else clause. This saves you from subtle, hard-to-debug problems, and is unambiguously readable. I see no reason you couldn't apply this reasoning to batch files.

    Let's take a look at your code.

    IF EXIST somefile.txt IF EXIST someotherfile.txt SET var=somefile.txt,someotherfile.txt
    

    And the IF syntax, from the command, HELP IF:

    IF [NOT] ERRORLEVEL number command
    IF [NOT] string1==string2 command
    IF [NOT] EXISTS filename command
    
    ...
    
    IF EXIST filename (
      command
    ) ELSE (
      other command
    )
    

    So you are chaining IF's as commands.

    If you use the common coding-standard rule I mentioned above, you would always want to use parens. Here is how you would do so for your example code:

    IF EXIST "somefile.txt" (
      IF EXIST "someotherfile.txt" (
        SET var="somefile.txt,someotherfile.txt"
      )
    )
    

    Make sure you cleanly format, and do some form of indentation. You do it in code, and you should do it in your batch scripts.

    Also, you should also get in the habit of always quoting your file names, and getting the quoting right. There is some verbiage under HELP FOR and HELP SET that will help you with removing extra quotes when re-quoting strings.

    Edit

    From your comments, and re-reading your original question, it seems like you want to build a comma separated list of files that exist. For this case, you could simply use a bunch of if/else statements, but that would result in a bunch of duplicated logic, and would not be at all clean if you had more than two files.

    A better way is to write a sub-routine that checks for a single file's existence, and appends to a variable if the file specified exists. Then just call that subroutine for each file you want to check for:

    @ECHO OFF
    SETLOCAL
    
    REM Todo: Set global script variables here
    CALL :MainScript
    GOTO :EOF
    
    REM MainScript()
    :MainScript
      SETLOCAL
    
      CALL :AddIfExists "somefile.txt" "%files%" "files"
      CALL :AddIfExists "someotherfile.txt" "%files%" "files"
    
      ECHO.Files: %files%
    
      ENDLOCAL
    GOTO :EOF
    
    REM AddIfExists(filename, existingFilenames, returnVariableName)
    :AddIfExists
      SETLOCAL
    
      IF EXIST "%~1" (
        SET "result=%~1"
      ) ELSE (
        SET "result="
      )
    
      (
        REM Cleanup, and return result - concatenate if necessary
        ENDLOCAL
    
        IF "%~2"=="" (
          SET "%~3=%result%"
        ) ELSE (
          SET "%~3=%~2,%result%"
        )
      )
    GOTO :EOF
    

提交回复
热议问题