Conditional Execution in Batch

11K518

Intro: 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

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 after command1. This is the same as:
command1command2
command1 && command2
Place two ampersands "&&" between two command to make command2 execute only if command1 finished 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 command1 fails. 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 after command1. 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 command1 finished 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 command1 fails. 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!

18 Comments

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)
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

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
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 ^|.
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:
^&^&
Thanks, I didn`t know that, and I just thought that you should update your Instructable with the info.
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!
I tested the clicky thing (from xdosemu),
and... It worked!
Ohhhhhhhhhh. It says IF (roflol rolling on floor laughing out loud)
did ya get my script?
wow i found something on batches that i didnt know. great job :]
Lol yah me too, and I thought I knew it all, I use batches all the time but this should help. thanks!
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.
Thank you for your comment! Check out all my other batch instructables.
very nice, shadow ops rocks!! i will get the PDF for this and use it soon :D thansk again and keep it up
Be sure to check out my other batch instructables, and remember to rate them!