loading
In the previous tutorial we learned the core essentials of writing batch files.

If you have no clue what batch is, but want to learn it, refer to the "Very Basic Batch Tutorial".

In this tutorial you will learn more advanced commands and how to use them to create an application.

Step 1: Variables Step (1/3)

Variables are things that are subject change.
When we create a variable, we are creating something that we want the computer to remember for us, something that we can use later on in a program;
we give the computer the value that we want to store and we give it a label to store it under.

we can create integers and strings using variables.

To create a variable you need to learn the SET command.

The SET command is what creates variables;

SET name=value

Type the following into your CMD:
SET name=hello

'name' is the name of the variable, and 'hello' is what the variable is storing

so now every time you type "echo name" it should say "hello" yes?
NO

if you want to display a variable you must put percentage (%) signs around it.

So therefore if you type "echo %test%" and it should say "hello" yes?
YES

Step 2: Variables Step (2/3)

So now surely we can do maths?

we type
set num=1
This creates a variable called "num" with a value of 1 attached to it.

then
set num=%num%+1
(this means that we take "num" (aka 1) and make it num+1 (aka 1+1))

then
echo %num%
it should give us 2, right?

let's try it:
type the following in notepad and save as MathAttempt.bat
(do not include the stars (*))
@echo off
set v=1
set v=%v%+1
echo %v%
pause
it should say 2, yes?
NOPE.
it says 1+1

because the computer interprets your command as:
you: "so num=1, right?"
pc: "right"
you: "so what is num plus one?"
pc: num+1 = 1+1
so the computer interprets your command literally.

Step 3: Variables Step (3/3)

So how do we get the computer to think mathematically?
Simple, we add an /a before the variable name

For example:

we type
"set /a num=1"

then
"set /a num=%num%+1"

then
"echo %num%"

then we should get 2, right?
let's try it
Type this into notepad....blah blah blah, you know the drill.
***************************************************
@echo off
set /a num=1
set /a num=%num%+1
echo %num%
pause
****************************************************
there! it added 1+1!
this is how the computer sees it:
_
you: so num=1, right?
pc: right
you: so what is num plus one?
pc: num+1 = 1+1 = 2
Voila!

So now lets make a counting program!
we will use the goto command that we learned about in the Very Basic Batch Tutorial.

*********************************************************
@echo off
set /a num=1
:top
set /a num=%num%+1
echo %num%
goto top
**********************************************************
The computer is adding 1, then going to the top and adding 1 again etc.

Step 4: Parameters Step (1/2)

So now that we can use variables what if we have a choice of options, like:
press 1 to say Hello.
press 2 to say Goodbye.

We use the "IF" command, for example:

Type this in your CMD:
if 1==1 echo See it works!
(==) means "is equal to", you could also type "EQU")
We got a message saying "See it works!"

Now type this:
if 1==2 echo It Works!
We didn't see anything because 1 does not equal 2

If we want to wait for the user to put something in we add a /p and leave the part after the variable empty.

Like this:
set /p variablename=

That means that the computer will wait for you to put in something.

so we type:
**************************************
@echo off
set v1=hi!!
set v2=bye!!
echo Press 1 to say HI!
echo Press 2 to say BYE!
set /p you=
if %you%==1 echo %v1%
if %you%==2 echo %v2%
pause
**************************************
This is telling the computer that if we type 1, it must echo HI!, and if we say 2 it must echo BYE!!

Step 5: Parameters Step (2/2)

So now we know that if we want to choose a variable we type:
set /p variablename=

and if we want to set a variable, we type:
set /a variablename=value

So now why not make a little program that counts to and from 2000?
We will use SET, IF and GOTO in this program (and obviously echo)
*************************************
@echo off
set /a num=0

:top
set /a num=%num%+1
echo %num%
if %num%==2000 goto goback
goto top

:goback
set /a num=%num%-1
echo %num%
if %num%==0 goto top
goto goback
**************************************
So now, whenever it reaches 2000, the IF command makes it GOTO the second part which makes it count down, then when it reaches 0, it will GOTO the first part which makes it count up...etc etc etc

Step 6: Done

You have finished my batch tutorials.
You can go here to go to another instructable for some Cool Batch Applications

If you would like help with any of your Batch programs, message me or send me an e-mail at jbell@live.co.za and I will try to help you.

If you want to try something offline, I recommend getting Learn Batch File Programming! by John Albert, really simple, easy to follow and great if you want to get better!

<p>This was really helpful for me, thanks!</p>
Okay, I just learned how to use this for the most part, but I just have one question... How do you get a batch file to open and run on your computer???
<p>You have to right-click a .bat file, then choose Run As Administrator. There you go.</p>
<p>You don't have to, you can just double click it or right-click and then press open. </p>
<p>just find where you saved the file and right click on it and then select open</p>
&nbsp;Double-click.........<br /> <br /> Is this your first computer?<br />
I&nbsp;don't remember making that comment, haha. Whoops.<br />
You fell asleep while your laptop was open, probably. My dad chatted on Skype while asleep one time. and it was not gibberish it was like, when he said he was just woke up on the 4th message, I was like, &quot;He was asleep?&quot;. And I have sleep talked before.
<p>cool! I have had a little experience with batch but not really deep I was mostly interested in it because I thought making little viruses was pretty cool but then I quit for a while and then I came to this tutorial and I used the start cammand in a loop kinda knowing what would happen and it crashed the computer in less than a second and then I said &quot;I think I just made a virus&quot; you can crash your computer with the following code:</p><p>@echo off</p><p>:a</p><p>start testqw.bat</p><p>goto a</p><p>as long as you are not running anything important that could get deleted when your computer shuts down not properly your computer will be fine it should shut itself down but if it doesn't just hit the power button on your computer.</p><p>Anyway, its fun learning batch!</p>
<p>oh i forgot to say you have to name the program testqw.bat for it to work</p>
Love it, nice man. Also using the &quot;if %you%==1&quot; command, made a game for my friends that if you get a question wrong you go to www.1227.com and if not, you continue...<br/><br/> -Cheers, Chris<br/><br/>
what is 1227.com
Its a rickashtely community that has every rick rolol link imaginable, and you CAN view them too, its not advised
Send them to tubgirl.com
Which is?????
Go there, it has this really funny picture on the front page.
<p>Alright, send them to https://akk.li/pics/anne/jpg for something nasty.</p>
<p>Very good tutorias</p>
<p>thank you!</p>
thank <em><strong>you!</strong></em>
<p>Is it possible that I use things that someone typed/filled in in the cmd? For example,</p><p>You let someone type his or her name on the cmd and somehow you can bring that up again with num= or anything else?</p>
<p>Yes, you can use the command...</p><p>@echo off</p><p>echo What is your name?</p><p>set /p name=</p><p>echo Your name is %name%.</p><p>pause</p><p>==============================================================</p><p>This command asks the user for their name, then prints their name on the screen.</p><p>PS.</p><p>Sorry I'm a little late. :P</p>
<p>@echo off</p><p>echo how old are you?</p><p>set/p age =</p><p>echo %age%</p><p>pause </p><p>for some reason this just tells me that &quot;Echo is off&quot;</p>
Oops forgot to finish... :O<br>I used the cls command to make it to where if you enter 1 it goes to the start and if you enter 2 it echo's goodbye and then exits right after you hit any key.<br><br>PS.<br>An echo doesn't stop the command so you do need the pause at the end.<br><br>PPS.<br>if you need more help with coding, go to Code Stop's you tube channel, that's where I learned the basics of batch.
<p>Another way to put a variable to use is...</p><p>@echo off</p><p>:start</p><p>cls</p><p>echo 1 = Hello!</p><p>echo 2 = Goodbye!</p><p>set /p a=</p><p>if %a% == 1 goto a</p><p>if %a% == 2 goto b</p><p>echo Incorrect input.</p><p>pause</p><p>goto start</p><p>:a</p><p>cls</p><p>echo Hello!</p><p>pause</p><p>goto start</p><p>:b</p><p>cls</p><p>echo Goodbye!</p><p>pause</p><p>exit</p><p>============================================================</p><p>This command allows you to have a cmd (command prompt) that asks you to type 1 or 2 and then hit enter. The if commands allow you to see which input you choose (1 or 2) and go to a certain location. The command under the if's and before the :a tells you if an incorrect input is at play. The rest of the command tells the user the specific input I added.</p>
<p>Or simply</p><p>@echo off</p><p>cls</p><p>set /p Name = Enter Your Name?</p><p>echo %Name%</p>
You need to have a pause command at the end so...<br>@echo off<br>set /p name=Enter Name:<br>echo %name%<br>pause<br>exit<br><br>But I like the more complex user inputs.<br>Thanks though!
<p>Hi admin,</p><p>I need help in making a calculator and the division part must have floating point integers. Any help please?</p><p>Thank you.</p>
<p>Hi admin,</p><p>I need help in making a calculator and the division part must have floating point integers. Any help please?</p><p>Thank you.</p>
<p>Hi admin,</p><p>I need help in making a calculator and the division part must have floating point integers. Any help please?</p><p>Thank you.</p>
<p>I used this tutorial and comments below to make a program to tell my GF how much I love her. Check it out :D</p><p>@echo off</p><p>color 1A</p><p>cls</p><p>echo I love you this much...</p><p>pause</p><p>set /a v=1</p><p>:top</p><p>set /a v=%v%+1</p><p>echo I love you this much %v%</p><p>if %v%==5000 goto one</p><p>if %v%==10000 goto two</p><p>if %v%==15000 goto three</p><p>if %v%==15010 goto four</p><p>if %v%==20000 goto five</p><p>if %v%==20010 goto six</p><p>if %v%==20020 goto seven</p><p>if %v%==20200 goto eight</p><p>if %v%==20300 goto nine</p><p>if %v%==20320 goto ten</p><p>goto top</p><p>:one</p><p>echo Wow that is a lot of love! Would you like to continue?</p><p>pause</p><p>goto top</p><p>:two</p><p>echo OMG that is a lot of love! Are you sure you want to continue?</p><p>pause</p><p>goto top</p><p>:three</p><p>echo Well, this is just a huge amount of love right here!</p><p>echo One wonders if there can be any more love in the world.</p><p>echo Are you sure you want to continue?</p><p>pause</p><p>goto top</p><p>:four</p><p>echo Seriously tho, are you sure you want this to go on?</p><p>echo There really is an infinite amount of love here...</p><p>pause</p><p>goto top</p><p>:five</p><p>echo Dude, that really is a lot of love isn't it!</p><p>echo Surely this is enough love for one person.</p><p>echo I suggest you quit now and do not continue</p><p>pause</p><p>goto top</p><p>:six</p><p>echo Fine, don't listen to me then.</p><p>pause</p><p>goto top</p><p>:seven</p><p>echo Aww, who am I kidding? Your independence just makes me love you more.</p><p>echo Want to see how much?</p><p>pause</p><p>goto top</p><p>:eight</p><p>echo Woah, I'm getting a bit too excited now.</p><p>echo Just give me a second to catch my breath.</p><p>pause</p><p>goto top</p><p>:nine</p><p>echo Hey, what do you think I am? A machine?</p><p>echo Just give me a minute and I will keep on loving you all night.</p><p>pause</p><p>goto top</p><p>:ten</p><p>echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz</p><p>pause</p><p>echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz</p><p>pause</p><p>echo zzzzzzzzzzzzz(yawn)... What?</p><p>echo No, I wasn't asleep baby. I was just resting my eyes.</p><p>pause</p><p>:eleven</p><p>set /a v=%v%-1</p><p>echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz %v%</p><p>if %v%==0 goto twelve</p><p>goto eleven</p><p>:twelve</p><p>echo (Yawn) Good morning baby. Guess how much I love you...</p><p>Pause</p><p>Goto top</p>
<p>Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast </p>
<p>Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast </p>
<p>Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast </p>
<p>Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast </p>
<p>IT HELPED ME A LOT THANKS FOR YOUR KNOWLEDGE</p>
<p>For useful Batch files visit</p><p><a href="http://batch798.blogspot.in/" rel="nofollow">http://batch798.blogspot.in/</a></p>
<p>I made a program to add, subtract, divide or multiply according to the user's wish. Check this out. If you have some suggestions, please tell</p>
<p>To display Multiplication Table of 3:</p><p>===========================================</p><p>@echo off</p><p>set a=1</p><p>set b=3</p><p>set n=11</p><p> :lb</p><p>if %a%==%n% goto end</p><p>set /a c = %b% *%a% </p><p>echo %b% x %a% = %c% </p><p>set /a a = %a%+1</p><p>goto lb</p><p>:end</p><p>pause</p><p>===============================================</p><p>output:</p><p> 3 x 1 = 3</p><p> 3 x 2 = 6</p><p> 3 x 3 = 9</p><p> 3 x 4 = 12</p><p> 3 x 5 = 15</p><p> 3 x 6 = 18</p><p> 3 x 7 = 21</p><p> 3 x 8 = 24</p><p> 3 x 9 = 27</p><p> 3 x 10 = 30</p><p>Press any key to continue . . .</p>
<p>hola ........ que puedo hacer o qu&eacute; c&oacute;digo a utilizar para un extracto de la informaci&oacute;n por lotes desde un servidor de base de datos SQL 2008 --- --- POR FAVOR AYUDA GRACIAS</p>
<p>hola........ como puedo hacer o que c&oacute;digo usar para que un batch extraiga informaci&oacute;n de una base de datos de SQL SERVER 2008 --- PORFAVOR AYUDENME --- GRACIAS</p>
<p>This Instructable inspired me to make one like it myself.<br>If you want to check it out....<br>https://www.instructables.com/id/Basic-Batch-command-list/</p>
I have a question I want it to do a goto comand when you type in 1 how do you do that<br/>so far I hav<br/>@echo off<br/>echo welcome to brian<br/>echo brian will now add 4 1698 times<br/>set/a num=4<br/>:furt<br/>set/a num=%num%+4<br/>echo %num%<br/>if %num%==6792 goto :end<br/>goto :furt<br/>:end<br/>echo you have reached 6792<br/>echo continue?<br/>set v1=goto :count1<br/>set v2=goto :g <br/>echo press 1 to continue<br/>echo press 2 to stop<br/>set/a nu=6792<br/>set/a n=0<br/>set /p you=<br/>if %you%==1 echo %v1% <br/>if %you%==2 echo %v2%<br/>
<p>@echo off</p><p>color 0a</p><p>title Brian the math batch program</p><p>echo Welcome to Brian</p><p>echo Brian will now add 4 1698 times</p><p>pause<br>set num=4</p><p><br>:furt<br>set /a num=%num% + 4<br>echo %num%<br>if %num%==6792 goto end</p><p>goto :furt<br></p><p>:end</p><p>color 0b</p><p>set v1=count1<br>set v2=g</p><p>set /a nu=6792<br>set /a n=0</p><p>cls</p><p>echo You have reached 6792<br>echo Continue?</p><p>echo.<br>echo Press 1 to continue<br>echo Press 2 to stop</p><p>set /p you=:<br>if %you%==1 goto %v1% <br>if %you%==2 goto %v2%</p>
<p>Well if you want it to go to a tag such as :main just do </p><p>set /p number =</p><p>if %number% == 1 goto main</p>
I fixed your code. <br> <br>@echo off <br>echo welcome to brian <br>echo brian will now add 4 1698 times <br>set /a num=4 <br>:furt <br>set /a num=%num%+4 <br>echo %num% <br>if %num%==g792 goto end <br>goto furt <br>:end <br>echo You have reached 6792 <br>echo continue? <br>echo press 1 to countinue <br>echo press 2 to stop <br>set /a nu=6792 <br>set /a n=0 <br>set /p you= <br>if %you%==1 goto count1 <br>if %you%==2 goto g <br> <br>
is this the whole program? if so, then i don't understand where %v1% and %v2% are meant to goto. but if u want it to work, take out the &quot;echo&quot; statements in the last two lines:<br/><hr/>if %you%==1 %v1%<br/>if %you%==2 %v2%<br/><hr/>
Hey man, I have a small problem. I'm tryign to make a .bat to begin a thing based of the %random% command. I'd put say.... 3485804 and if it goes to it, it begin a "Insert thing evil program here". I presumed this would work @echo off title Random Number Gen by seJma color 2f :top cls echo Your Number is... echo %random% if %1227% goto begin goto top :begin pause start www.1227.com
Nevermind<br/>i used the if %random%==1227 goto begin<br/>

About This Instructable

167,614views

39favorites

License:

More by Sejma:Very Basic Batch Tutorial Not So Basic Batch Tutorial Some Cool Batch Applications 
Add instructable to: