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
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 anelse
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.
And the
IF
syntax, from the command,HELP IF
: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:
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
andHELP 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:
The explanation given by Merlyn above is pretty complete. However, I would like to elaborate on coding standards.
When several IF's are chained, the final command is executed when all the previous conditions are meet; this is equivalent to an AND operator. I used this behavior now and then, but I clearly indicate what I intend to do via an auxiliary Batch variable called AND:
Of course, this is NOT a true And operator and must not be used in combination with ELSE clause. This is just a programmer aid to increase the legibility of an instruction that is rarely used.
When I write Batch programs I always use several auxiliary variables that I designed with the sole purpose of write more readable code. For example:
These variables aids in writting Batch programs in a much clearer way and helps to avoid subtle errors, as Merlyn suggested. For example:
I even have variables that aids in writting WHILE-DO and REPEAT-UNTIL like constructs. This means that Batch variables may be used in some degree as preprocessor values.
You can structurize your batch file by using goto
Is this feasible?
Batch files have really very limited logic powers so the best you can hope to come up with is a good workaround that indirectly achieves what you want. That's not to say that you should feel they are inferior to a real language - they still demand the same attention to detail and manual debugging as a real application. It's just that you'll need to work a lot harder to make them do what you want in a robust manner.
For the OP's question it sounds like you require two specific files to exist. Just use a tally:
My example uses 3 variables, but you could just add 1 to file_result_status if the file exists. But if you want more granular control later in your batch file you can record the result for each file as I have done so you don't have to keep checking if a file exists later on.