Instructables
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.
 
Remove these adsRemove these ads by Signing Up

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)

Picture of 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.
admin6 years ago
Hello, and welcome to the Instructables community! It's great that you've decided to tell the world about something you've made by publishing an Instructable. We just wanted to let you know that your project still needs a little more work if you want it to be well received on Instructables. Projects that don't include certain basic elements tend not to get the attention that they deserve, and so we'd love for you to check out the list below of what makes a successful Instructable. Successful projects on Instructables include: - clearly written details of a finished project with instruction - as many steps as are necessary to explain your project - clear images that you took of your project for most, if not all of your steps - an intro image - proper spelling and grammar - appropriate cautions or safety considerations I'll give you another opportunity to make any final changes to your project before we publish it. Once you're all set to go, please republish your project and send me a quick comment letting me know that you've made some changes. I'll give it a quick final check to make sure you're on the right path, and then remove this note. Thanks for your submission and we hope to see your project published soon!
meispicklu5 days ago

Nice tutorials. I completed both of two for beginers.

ttani14 months ago

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

Lance Mt.5 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
Nevermind
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"?
Even though it's old...

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

That method uses modulo expressions to constrain the number.

Example:
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.
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
: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%
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

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%

edfel015 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.
erikals5 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....
CapnTac5 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???
 Double-click.........

Is this your first computer?
I don't remember making that comment, haha. Whoops.
will421 CapnTac4 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.
kalimanni4 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)  YummyPancakes6 years ago
haha, very observant......i'll change it just now
Deadkirby5 years ago
This is a bit wird but variables r easy
lobo_pal5 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.5 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.5 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.5 years ago
what is 1227.com
Lance Mt. Derin5 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.
Arbitror6 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)  Arbitror6 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 Sejma6 years ago
OK!
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.
QwertyuioLP6 years ago
Could you create a random number generator then?
Sejma (author)  QwertyuioLP6 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
thnks
shouldn't it be parameters not perimeters.
Sejma (author)  DavidRobertson6 years ago
right you are.