Introduction: Not So Basic Batch Tutorial

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!

Comments

author
PaulD370 made it! (author)2017-06-12

This was really helpful for me, thanks!

1-2.png3.png
author
CapnTac made it! (author)2009-07-26

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

author
SubratM3 made it! (author)SubratM32016-12-27

You have to right-click a .bat file, then choose Run As Administrator. There you go.

author
kaikaicoolguy22 made it! (author)kaikaicoolguy222017-03-20

You don't have to, you can just double click it or right-click and then press open.

author
BuildingBlox made it! (author)BuildingBlox2017-01-16

just find where you saved the file and right click on it and then select open

author
Guard13007 made it! (author)Guard130072010-04-10

 Double-click.........

Is this your first computer?

author
CapnTac made it! (author)CapnTac2010-04-11

I don't remember making that comment, haha. Whoops.

author
will421 made it! (author)will4212010-07-22

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, "He was asleep?". And I have sleep talked before.

author
BuildingBlox made it! (author)2017-01-16

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 "I think I just made a virus" you can crash your computer with the following code:

@echo off

:a

start testqw.bat

goto a

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.

Anyway, its fun learning batch!

author
BuildingBlox made it! (author)BuildingBlox2017-01-16

oh i forgot to say you have to name the program testqw.bat for it to work

author
Lance Mt. made it! (author)2009-02-11

Love it, nice man. Also using the "if %you%==1" 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...

-Cheers, Chris

author
Derin made it! (author)Derin2009-02-12

what is 1227.com

author
Lance Mt. made it! (author)Lance Mt.2009-02-15

Its a rickashtely community that has every rick rolol link imaginable, and you CAN view them too, its not advised

author
lobo_pal made it! (author)lobo_pal2009-02-19

Send them to tubgirl.com

author
Lance Mt. made it! (author)Lance Mt.2009-02-21

Which is?????

author
lobo_pal made it! (author)lobo_pal2009-02-21

Go there, it has this really funny picture on the front page.

author
SubratM3 made it! (author)SubratM32016-12-27

Alright, send them to https://akk.li/pics/anne/jpg for something nasty.

author
IsraelN8 made it! (author)2016-10-08

Very good tutorias

author
darthvardhan made it! (author)2016-08-11

thank you!

author
Sejma made it! (author)Sejma2016-08-12

thank you!

author
RobbyV2 made it! (author)2016-02-27

Is it possible that I use things that someone typed/filled in in the cmd? For example,

You let someone type his or her name on the cmd and somehow you can bring that up again with num= or anything else?

author
Blake Turner made it! (author)Blake Turner2016-05-31

Yes, you can use the command...

@echo off

echo What is your name?

set /p name=

echo Your name is %name%.

pause

==============================================================

This command asks the user for their name, then prints their name on the screen.

PS.

Sorry I'm a little late. :P

author
alricb made it! (author)alricb2016-07-25

@echo off

echo how old are you?

set/p age =

echo %age%

pause

for some reason this just tells me that "Echo is off"

author
Blake Turner made it! (author)2016-06-01

Oops forgot to finish... :O
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.

PS.
An echo doesn't stop the command so you do need the pause at the end.

PPS.
if you need more help with coding, go to Code Stop's you tube channel, that's where I learned the basics of batch.

author
Blake Turner made it! (author)2016-05-31

Another way to put a variable to use is...

@echo off

:start

cls

echo 1 = Hello!

echo 2 = Goodbye!

set /p a=

if %a% == 1 goto a

if %a% == 2 goto b

echo Incorrect input.

pause

goto start

:a

cls

echo Hello!

pause

goto start

:b

cls

echo Goodbye!

pause

exit

============================================================

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.

author
XavierF9 made it! (author)XavierF92016-05-31

Or simply

@echo off

cls

set /p Name = Enter Your Name?

echo %Name%

author
Blake Turner made it! (author)Blake Turner2016-06-01

You need to have a pause command at the end so...
@echo off
set /p name=Enter Name:
echo %name%
pause
exit

But I like the more complex user inputs.
Thanks though!

author
XavierF9 made it! (author)2016-05-31

Hi admin,

I need help in making a calculator and the division part must have floating point integers. Any help please?

Thank you.

author
XavierF9 made it! (author)2016-05-31

Hi admin,

I need help in making a calculator and the division part must have floating point integers. Any help please?

Thank you.

author
XavierF9 made it! (author)2016-05-31

Hi admin,

I need help in making a calculator and the division part must have floating point integers. Any help please?

Thank you.

author
MatthewR119 made it! (author)2016-04-25

I used this tutorial and comments below to make a program to tell my GF how much I love her. Check it out :D

@echo off

color 1A

cls

echo I love you this much...

pause

set /a v=1

:top

set /a v=%v%+1

echo I love you this much %v%

if %v%==5000 goto one

if %v%==10000 goto two

if %v%==15000 goto three

if %v%==15010 goto four

if %v%==20000 goto five

if %v%==20010 goto six

if %v%==20020 goto seven

if %v%==20200 goto eight

if %v%==20300 goto nine

if %v%==20320 goto ten

goto top

:one

echo Wow that is a lot of love! Would you like to continue?

pause

goto top

:two

echo OMG that is a lot of love! Are you sure you want to continue?

pause

goto top

:three

echo Well, this is just a huge amount of love right here!

echo One wonders if there can be any more love in the world.

echo Are you sure you want to continue?

pause

goto top

:four

echo Seriously tho, are you sure you want this to go on?

echo There really is an infinite amount of love here...

pause

goto top

:five

echo Dude, that really is a lot of love isn't it!

echo Surely this is enough love for one person.

echo I suggest you quit now and do not continue

pause

goto top

:six

echo Fine, don't listen to me then.

pause

goto top

:seven

echo Aww, who am I kidding? Your independence just makes me love you more.

echo Want to see how much?

pause

goto top

:eight

echo Woah, I'm getting a bit too excited now.

echo Just give me a second to catch my breath.

pause

goto top

:nine

echo Hey, what do you think I am? A machine?

echo Just give me a minute and I will keep on loving you all night.

pause

goto top

:ten

echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz

pause

echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz

pause

echo zzzzzzzzzzzzz(yawn)... What?

echo No, I wasn't asleep baby. I was just resting my eyes.

pause

:eleven

set /a v=%v%-1

echo zzzzzzzzzzzzzzzzzzzzzzzzzzzz %v%

if %v%==0 goto twelve

goto eleven

:twelve

echo (Yawn) Good morning baby. Guess how much I love you...

Pause

Goto top

author
XavierF9 made it! (author)XavierF92016-05-31

Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast

author
XavierF9 made it! (author)XavierF92016-05-31

Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast

author
XavierF9 made it! (author)XavierF92016-05-31

Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast

author
XavierF9 made it! (author)XavierF92016-05-31

Well, unless you print the numbers generated in v and unless she has a slow Pc, this will be fast

author
VARUN360 made it! (author)2016-05-18

IT HELPED ME A LOT THANKS FOR YOUR KNOWLEDGE

author
Pratik Sonigra made it! (author)2016-03-26

For useful Batch files visit

http://batch798.blogspot.in/

author
Kritik333 made it! (author)2016-02-15

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

Program1_1.PNGProgram1_2.PNGProgram1_3.PNG
author
AkashJ12 made it! (author)2015-10-31

To display Multiplication Table of 3:

===========================================

@echo off

set a=1

set b=3

set n=11

:lb

if %a%==%n% goto end

set /a c = %b% *%a%

echo %b% x %a% = %c%

set /a a = %a%+1

goto lb

:end

pause

===============================================

output:

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

Press any key to continue . . .

author
LolaL3 made it! (author)2015-09-24

hola ........ que puedo hacer o qué código a utilizar para un extracto de la información por lotes desde un servidor de base de datos SQL 2008 --- --- POR FAVOR AYUDA GRACIAS

author
LolaL3 made it! (author)2015-09-24

hola........ como puedo hacer o que código usar para que un batch extraiga información de una base de datos de SQL SERVER 2008 --- PORFAVOR AYUDENME --- GRACIAS

author
LolaL3 made it! (author)2015-09-24

author
Mr_ MikaS made it! (author)2015-09-18

This Instructable inspired me to make one like it myself.
If you want to check it out....
https://www.instructables.com/id/Basic-Batch-command-list/

557b3d6e15be4dac6f00030b.jpegphoto.png
author
goosegg34 made it! (author)2009-09-20

I have a question I want it to do a goto comand when you type in 1 how do you do that
so far I hav
@echo off
echo welcome to brian
echo brian will now add 4 1698 times
set/a num=4
:furt
set/a num=%num%+4
echo %num%
if %num%==6792 goto :end
goto :furt
:end
echo you have reached 6792
echo continue?
set v1=goto :count1
set v2=goto :g
echo press 1 to continue
echo press 2 to stop
set/a nu=6792
set/a n=0
set /p you=
if %you%==1 echo %v1%
if %you%==2 echo %v2%

author
Mr_ MikaS made it! (author)Mr_ MikaS2015-09-14

@echo off

color 0a

title Brian the math batch program

echo Welcome to Brian

echo Brian will now add 4 1698 times

pause
set num=4


:furt
set /a num=%num% + 4
echo %num%
if %num%==6792 goto end

goto :furt

:end

color 0b

set v1=count1
set v2=g

set /a nu=6792
set /a n=0

cls

echo You have reached 6792
echo Continue?

echo.
echo Press 1 to continue
echo Press 2 to stop

set /p you=:
if %you%==1 goto %v1%
if %you%==2 goto %v2%

author
VictorS10 made it! (author)VictorS102015-08-10

Well if you want it to go to a tag such as :main just do

set /p number =

if %number% == 1 goto main

author
Prof. Pickle made it! (author)Prof. Pickle2012-08-28

I fixed your code.

@echo off
echo welcome to brian
echo brian will now add 4 1698 times
set /a num=4
:furt
set /a num=%num%+4
echo %num%
if %num%==g792 goto end
goto furt
:end
echo You have reached 6792
echo continue?
echo press 1 to countinue
echo press 2 to stop
set /a nu=6792
set /a n=0
set /p you=
if %you%==1 goto count1
if %you%==2 goto g

author
Sejma made it! (author)Sejma2009-09-20

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 "echo" statements in the last two lines:


if %you%==1 %v1%
if %you%==2 %v2%

author
Lance Mt. made it! (author)2009-02-27

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

author
Lance Mt. made it! (author)Lance Mt.2009-02-27

Nevermind
i used the if %random%==1227 goto begin

About This Instructable

168,544views

40favorites

License:

More by Sejma:Very Basic Batch TutorialNot So Basic Batch TutorialSome Cool Batch Applications
Add instructable to: