Not So Basic Batch Tutorial
Intro: Not So Basic Batch Tutorial
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)
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)
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)
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)
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!
88 Comments
Cru5y 5 years ago
"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?
skylar.herman 4 years ago
@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.
LoneWolf125 6 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
kinglarry II 6 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
Fearce1 6 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
kinglarry II 6 years ago
num=1
num=%num%+1 would've worked in python IDLE why does the computer interpret it differently in python
thomasgamer4000 7 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
CapnTac 15 years ago
SubratM3 8 years ago
You have to right-click a .bat file, then choose Run As Administrator. There you go.
Kai_Dijkstra 8 years ago
You don't have to, you can just double click it or right-click and then press open.
BuildingBlox 8 years ago
just find where you saved the file and right click on it and then select open
Guard13007 15 years ago
Is this your first computer?
CapnTac 15 years ago
will421 14 years ago
BuildingBlox 8 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!
BuildingBlox 8 years ago
oh i forgot to say you have to name the program testqw.bat for it to work
Lance Mt. 16 years ago
-Cheers, Chris
Derin 16 years ago
Lance Mt. 16 years ago
lobo_pal 16 years ago