Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6162695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:41:51+00:00 2026-05-23T21:41:51+00:00

When using more than 1 IF statement, is there a special guideline that should

  • 0

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 example to use would be:

IF EXIST somefile.txt IF EXIST someotherfile.txt SET var=somefile.txt,someotherfile.txt
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:41:52+00:00Added an answer on May 23, 2026 at 9:41 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to clamp real numbers than using if statements
Is it possible to declare more than one variable using a with statement in
Is there a way to update more than one Database having same schema using
How do I query posts from a category using more than 1 custom key/value
The HTTP 1.1 RFC restricts a Client from using more than Two TCP connections
I still find myself hand coding Visual Studio projects more than using the variety
I am using oracle DB to maintain more than 30 tables, how can I
Using the standard win32 api, what's the best way to detect more than one
I'm using jsf 1.2. When a particular jsp has more than one form with
I'm using Fluent NHibernate auto mapping. I need to access more than one database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.