Not So Basic Batch Tutorial





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?

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?

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.

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

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%
it should say 2, yes?
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"

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

"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%
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

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

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

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

3 People Made This Project!


  • Paper Contest 2018

    Paper Contest 2018
  • Pocket-Sized Contest

    Pocket-Sized Contest
  • Science of Cooking

    Science of Cooking

We have a be nice policy.
Please be positive and constructive.




how are variables not one of the 3 core elements? i would say the 3 core elements in batch scripting are:




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

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

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

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


Is this your first computer?

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

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.

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


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!

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