Not So Basic Batch Tutorial

180,389

40

87

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

4 People Made This Project!

Recommendations

  • Fix It! Contest

    Fix It! Contest
  • Tiny Home Contest

    Tiny Home Contest
  • Furniture Contest 2018

    Furniture Contest 2018

87 Discussions

0
None
LoneWolf125

Question 3 months 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

2 more answers
0
None
kinglarry IILoneWolf125

Answer 5 weeks 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
None
Fearce1LoneWolf125

Answer 3 months 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
None
kinglarry II

5 weeks 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
None
thomasgamer4000

11 months 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
None
CapnTac

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

6 replies
0
None
SubratM3CapnTac

Reply 1 year ago

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

0
None
Kai_DijkstraSubratM3

Reply 1 year ago

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

0
None
BuildingBloxCapnTac

Reply 1 year ago

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

0
None
will421CapnTac

Reply 8 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
None
BuildingBlox

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

1 reply
0
None
BuildingBloxBuildingBlox

Reply 1 year ago

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

0
None
Lance Mt.

9 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

5 replies
0
None
Lance Mt.Derin

Reply 9 years ago on Introduction

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