Picture of 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?
goosegg345 years ago
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
set/a num=%num%+4
echo %num%
if %num%==6792 goto :end
goto :furt
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%

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

set /p number =

if %number% == 1 goto main

I fixed your code.

@echo off
echo welcome to brian
echo brian will now add 4 1698 times
set /a num=4
set /a num=%num%+4
echo %num%
if %num%==g792 goto end
goto furt
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

Sejma (author)  goosegg345 years ago
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%

Lance Mt.6 years ago
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
i used the if %random%==1227 goto begin
How do i limit the number? Is the a "equal to or less than" or just "less than"?
MikaS5 Lance Mt.2 months ago

set /a a=%random% %%!(limit of numbers that can be chosen) +1
example: set /a a=%random% %%!10 +1
it sets "a" to equal a random number between 0 and 10

Even though it's old...

SET /a Var=%RANDOM% %%max + min

That method uses modulo expressions to constrain the number.

If "max" had the value of 10, and "min" had the value of 1...
The variable "Var" would be a number between 1 and 10.


JosephM116 months ago

Amazing! :D Really funny and helpful tutorial!

Jay2427 months ago

What does /a mean?

Jay2427 months ago

Can you please explain to me what /p means??

amits78 months ago

hey admin,

I need to prepare a batch file to set Security policy in local policy of my systems

Can you help in this regards



meispicklu10 months ago

Nice tutorials. I completed both of two for beginers.

ttani11 year ago

this was really awesome, I got to learn a lot from both of your tutorials, this one & that of the basic one :)

edfel016 years ago
im very new to batch and i want to know everything. how do u open up .bat files on notepad and how do u create an account and the number thing wont work for me. plz help
 You can't open them in Notepad without "hacking" your registry, a much simpler idea is to install Notepad++ (Google it, don't feel like putting a link) and it's better cause Notepad++ has syntax highlighting.
It's called Right-click, EDIT.
erikals6 years ago
Your random number generator is great, though one problem. It increments in 10s instead of generating random numbers. Do you think you can fix this? Otherwise, a really excellent Instructable.
if you think about it, a random number generator is virtually impossible. it will have to follow some sort of pattern or otherwise create something that changes the pattern itself and even THAT is considered a pattern....
CapnTac6 years ago
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???

Is this your first computer?
I don't remember making that comment, haha. Whoops.
will421 CapnTac5 years ago
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.
kalimanni5 years ago
can i do the same thing so it will count letters and numbers such as for running through various combinations of words/ digits?
I pressed 2, and it said "BYE!!!" instead of "BI!" What a rip-off.
lol thats not a rip-off lol.
Sejma (author)  YummyPancakes7 years ago
haha, very observant......i'll change it just now
Deadkirby6 years ago
This is a bit wird but variables r easy
lobo_pal6 years ago
Agh, my brain. splat.
Nah, more like plop. Unless you watch a lot of TV. Then it's ker-plunk (slurp). Sorry, been watching too many hulu commercials.
Ha! Like your icon (It`s not an avatar rofl)
Lance Mt.6 years ago
Excuse me admin, This is a part 3 of 3, i don't think it needs much more but if you insist. -Just thinking, Chris
Lance Mt.6 years ago
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

Derin Lance Mt.6 years ago
what is 1227.com
Lance Mt. Derin6 years ago
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.
Arbitror7 years ago
Sorry, but NeoDudeMan beat you to this instructable! He has already made a basic, intermediate, and advanced instructables! Your is almost the same... I wonder if there is any copy'n'pasting involved...
Sejma (author)  Arbitror7 years ago
yeah his tutorials are pretty good, but i'm not a "copy, paste" type of guy, I just wanted to explain batch through my eyes.
Arbitror Sejma7 years ago
So you have already met Sejma, then? you acted as if he was new to you! P.S. Show us how you made the flash drive backup, please.
I doubt he stole any ideas, because batch file tutorials can't really be made too uniquely.
QwertyuioLP7 years ago
Could you create a random number generator then?
Sejma (author)  QwertyuioLP7 years ago
yes, try this (you can take out the "cls" if you want) @echo off title Random Number Gen by seJma color 2f :top cls echo %random% pause goto top
shouldn't it be parameters not perimeters.
Sejma (author)  DavidRobertson7 years ago
right you are.