Introduction: Conditional Execution in Batch

Picture of Conditional Execution in Batch

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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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!

Comments

dla888 (author)2009-06-08

Hmmm Makes me think of a logic bomb. For those of you who haven't had to deal with viruses carrying this payload, a logic bomb is a set of instructions that are executed when certain variables are met. (i.e. When it reaches december 12 the virus erases your HD)

knex_mepalm (author)dla8882009-10-13

I'm planning to do something like that, but instead, it sends out a  greeting, i don't know the code for doing this tough.

Refer to Festive Batching for this

knex_mepalm (author)2009-10-13

I just use this:
@echo off
echo hi would you like to kill your mum Y/N
set/p "kill=>"
if %kill%==Y goto y
if %kill%==y goto y
if %kill%==N goto n
if %kill%==n goto n
:y
echo your mum is dead
:n
echo your mum is dead, not your fault

will421 (author)2009-06-15
Hey, Shadow Ops, you can also use the
^
symbol if you don`t want the >, >>, &, && and || to do their thing. For example:
echo hi ^<Html tag^> go ^>> ^& ^> my ^&& and my ^|| and my ^|.
Arbitror (author)will4212009-06-15
I already know how to do that! But, for two pipes, or two "special characters", you can't go like this:
^&&
You must go like this:
^&^&
will421 (author)Arbitror2009-06-16

Thanks, I didn`t know that, and I just thought that you should update your Instructable with the info.

higginsiv (author)2009-05-14

hey yall should check out my batches at higginsbatch.webs.com you can download them there, i have a lot of batch games tell me what you think!

will421 (author)2009-04-18

I tested the clicky thing (from xdosemu),
and... It worked!

will421 (author)will4212009-04-27

Ohhhhhhhhhh. It says IF (roflol rolling on floor laughing out loud)

fwjs28 (author)2009-03-25
Arbitror (author)fwjs282009-03-25

Ha LOL!

fwjs28 (author)Arbitror2009-03-25

did ya get my script?

higginsiv (author)2009-01-30

wow i found something on batches that i didnt know. great job :]

Spike002 (author)higginsiv2009-02-01

Lol yah me too, and I thought I knew it all, I use batches all the time but this should help. thanks!

flawd1 (author)2008-11-08

Thanks for this one! I use a batch file for installing 10-15 programs silently for work, and this will help me keep it a little more organized.

Arbitror (author)flawd12008-12-08

Thank you for your comment! Check out all my other batch instructables.

spiker (author)2008-11-13

very nice, shadow ops rocks!! i will get the PDF for this and use it soon :D thansk again and keep it up

Arbitror (author)spiker2008-12-08

Be sure to check out my other batch instructables, and remember to rate them!

About This Instructable

4,030views

5favorites

License:

Bio: 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 ... More »
More by Arbitror:Voltage HorizonMy AquariumOcean Storm Oil Painting
Add instructable to: