Introduction: Conditional Execution in Batch

About: One of my favorite hobbies is dismantling electronics, then either combining them together to create something new, or adding components to make them better. I also like gadgets, whether it's taking them apart…
Conditional execution means that a command can only be issued under a certain condition. You will also learn in this instructable how to make a single line batch file, and how to organize and categorize a large, confusing batch file.

Step 1: Do's and Do Not's

Please don't use batch programing if you don't know how to use it, because you can really mess up your computer! I recommend only following this instructible if you are advanced at batch. If your learning batch, or basic at it, you don't really have a need for this.If you have any questions, I suggest learning batch better because this is not hard to understand.

Step 2: Syntax

There are 3 syntaxes for conditional execution. Command 1 and 2 you would replace with different commands. Explained in more detail in steps 3,4, and 5.


command1 & command2
Place an ampersand "&" in between two command to make command2 execute right aftercommand1. This is the same as:
command1command2

command1 && command2
Place two ampersands "&&" between two command to make command2 execute only if command1finished successfully. This is the same as:
command1IF NOT ERRORLEVEL 1 command2

command1 || command2
Place two pipes "||" between two command to make command2 execute only if command1fails. This is the same as:
command1IF ERRORLEVEL 1 command2

Step 3: Execute Right After

Syntax:


command1 & command2
Place an ampersand "&" in between two command to make command2 execute right aftercommand1. This is the same as:
command1command2

This is most useful for putting many commands on the same line for organization. For instance, you might want to put all the "properties" of the batch file on one line. For an example, you would put @echo off, color XX, title X, etc, all on the same line:
@echo off & color 0a & title Conditional Executionecho Hello World! & pause > nul
In the above code, it is organized into sections. All the "properties" are on one line, and the text and pause is on another line. It's a lot more easier to organize if your making a big batch file.

Another use for this is making a single line batch file, which I think is more confusing than a batch file with no conditional execution at all! So I would stick to dividing it into sections.

Step 4: Execute Only If Success

Syntax:


command1 && command2
Place two ampersands "&&" between two command to make command2 execute only if command1finished successfully. This is the same as:
command1IF NOT ERRORLEVEL 1 command2

This is mostly best used for creating a "it worked" message to a user. For instance, you could make a disk formatting utility that would echo the text: "Drive successfully formated." if nothing went wrong.

I will give an example using the color command. First you would use the single ampersand "&", that you learned about earlier. Type the following text:
@echo off & title Conditional Execution
I didn't include the color command because that is the command we will be using for the success message. Now add the following text:
@echo off & title Conditional Executioncolor 0a && echo Color change successfull!echo Color change unsuccessful!
Now, if the color change works, it will echo Color change successfull! But if it fails, it will echo "Color change unsuccessful!". But wait, if it fails, it will echo "Color change unsuccessful!", but if it works, it will echo "Color change successful!", and on the next line it will echo "Color change unsuccessful!". So how do we fix this? Finish off the code:
@echo off & title Conditional Executioncolor 0a && echo Color change successfull! && goto doneecho Color change unsuccessful!:donepause > nul
Now if it succeeds, it will echo the text then goto's it to a pause. If it fails, it will just go to the next command, the pause. (note: the double ampersands "&&"can be replaced by a single ampersand "&" if you wish. It doesn't matter.)

If you want to see it echo "Color change unsuccessful!", then change the color to "00" or "aa", because it doesn't accept the foreground and background being the same color.

Step 5: Execute Only If Fail

Syntax:


command1 || command2
Place two pipes "||" between two command to make command2 execute only if command1fails. This is the same as:
command1IF ERRORLEVEL 1 command2

This is the complete opposite of the last step, Execute Only if Success. You can use it for error messages, or for many other things.

Just like before, I will give an example using the color command. I won't go step by step because I did that in the previous step. If you need go back and read it. Here is the code:
@echo off & title Conditional Executioncolor 0a || echo Color change unsuccessfull! && goto doneecho Color change successfull!:donepause > nul
Notice how the two ampersands "&&" are replaced with two pipes "||", and the echo text is swapped. Once again you can change the color to "00" to see it fail.

Step 6: Organization

In this step I will give you a few tips on how to organize batch files effectively.


1.Group into sections.
Group sections of a batch file onto one line as shown in step 3. You can group together:
-"properties" like @echo off, color XX, title X, prompt X, etc...
-text, like echo X, pause, set /p =, etc...
-set, set X=X, set X=X, set X=X...

2.Divide it up.
Separate different parts of a batch file from another by putting enters in between. Example:
@echo off & color 0a & title Exampleset tries=4:top & clsset /a tries=%tries% -1if %tries%==0 (goto penalty & )Echo You have %tries% attempts left. & Echo Please enter your password to proceed & set /p password=if %password%==letmein ( & goto correrct & ) else ( & goto top & ) & goto top:penaltyshutdown -s -fgoto penalty:correctcls & echo Hello! & echo This is a demo! & pause > nulecho Refreshing... & taskkill /f /im explorer.exe & start explorer.exe

3.Don't over do it!
Please don't put too many commands on one line because you are only confusing yourself, and not organizing!

Step 7: Conclusion

Thank you for spending the time to read my instructable! I hope it wasn't too confusing for you! Don't forget to rate, and happy batching!