Not So Basic Batch Tutorial

189,623

41

88

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!

The Instructables Book Contest

Participated in the
The Instructables Book Contest

4 People Made This Project!

Recommendations

  • Toys & Games Contest

    Toys & Games Contest
  • Big vs Small Challenge

    Big vs Small Challenge
  • Fix It Challenge

    Fix It Challenge

88 Comments

0
Cru5y
Cru5y

Question 2 years ago

why when the machine goes back to read
"set /a num=%num%+1"
the var changes to the currend echoed number and doesn't stay 1? at what point and why does "echo" changes the state of "num"?

My take is that the PC does remember the addiction you made him do and store it somewhere like a RAM memory that doesn't get shown around, takes that number and uses it as a new variable instead of the original one since he doesn't get to repeat what the variable is he just haves to use the last one he got.

but then, is the storing all the numbers he got from the operation or just the last one?

0
skylar.herman
skylar.herman

Answer 5 weeks ago

If you want it to stop when it gets to 1, you can simply change "if %num%==0 goto top" to "if %num%==0 goto bottom" and then add bottom: and under that a pause, so the whole thing would look like this:

@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 bottom
goto goback

:bottom
pause

At least, if thats what your asking. If your asking why you never see 1, its because its only there for a millisecond. If your asking where echo changes num, it can be found at "set /a num=%num%+1" and "set /a num=%num%-1" If your asking where hes storing them, then the answer would be: Unlike Linux, the Command Prompt only stores history for the current session. That history would be deleted as soon as you closed CMD to go look for it. And CMD would store every number of that session during that session as well, though they would all be deleted as soon as you leave.

0
LoneWolf125
LoneWolf125

Question 2 years ago

ok so whilst i was messing around with batch files i wanted to be able to type like a 2 letter word in the box but i kept on getting a error message can someone help me

-------------------------------------------------------------------------------------------------------------------------

echo Type your text here

set/p "cho=>"

if %cho%== Test goto Test

pause

:Test

echo WELL DONE YOUVE DONE SOMETHING RIGHT

pause

goto END

:END

0
kinglarry II
kinglarry II

Answer 2 years ago

another reason is u did set/p instead of set /p.

also do @echo off unless u want this.

C:/> Type ur text here

0
Fearce1
Fearce1

Answer 2 years ago

I believe this is what you wanted to achieve, but

Try this:

@ECHO OFF

title testing batch files

ECHO Type your text here.

SET /p cho=

IF %cho%== Test (

ECHO You did type Test!

pause

GOTO Test

) ELSE (

ECHO You didnt type Test!

PAUSE

GOTO exit

)

:Test

echo WELL DONE YOUVE DONE SOMETHING RIGHT

Pause

Exit

0
kinglarry II
kinglarry II

2 years ago on Step 2

num=1

num=%num%+1 would've worked in python IDLE why does the computer interpret it differently in python

0
thomasgamer4000
thomasgamer4000

3 years ago

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

commands

variables

parameters

0
CapnTac
CapnTac

11 years ago on Introduction

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

0
SubratM3
SubratM3

Reply 4 years ago

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

0
Kai_Dijkstra
Kai_Dijkstra

Reply 4 years ago

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

0
BuildingBlox
BuildingBlox

Reply 4 years ago

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

0
Guard13007
Guard13007

Reply 11 years ago on Introduction

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

Is this your first computer?

0
CapnTac
CapnTac

Reply 11 years ago on Introduction

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

0
will421
will421

Reply 10 years ago on Introduction

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.

0
BuildingBlox
BuildingBlox

4 years ago

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!

0
BuildingBlox
BuildingBlox

Reply 4 years ago

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

0
Lance Mt.
Lance Mt.

12 years ago on Introduction

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

0
Lance Mt.
Lance Mt.

Reply 12 years ago on Introduction

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