CMD (Batch File) Programming Walkthrough

The basics in programming using the batch file interface. Important – in this I give you examples of code, DO NOT copy and paste into note pad. IT MIGHT NOT WORK. Just copy by re-typing it into note pad. With the start of each chapter open a new word pad and save the old stuff you learned if you wish.

Chapter 1, the basics

First let’s talk about Batch files. Batch files are used to do simple or repetitive tasks. They are similar to the DOS interface if you are familiar with that.

What you need

  • A Computer (duh)
  • A typing program (Notepad)
  • Internet Connection (optional)
  • And the will to learn (YAY LEARNING!)
  • Windows XP (that’s what I’m using, but other windows should work)

Get right to it

Open up note pad first of all to stop the annoying echo of your computer name (type not sound) we will type in the following code into the text box, “@echo off” (minus quotations. Press enter (or return) to go to the next line

Coding

Now let’s add a simple code “echo Hello World!” (Not quotations) Press enter (or return) on your keyboard to go to the next line. Now type “pause” (still no quotations).

It should look like this image above

Making the file

To create the batch file, in notepad, click file, then click save as… In the new menu that popped up, click in the box called file name, and type the following (after deleting what’s in there)” Hello_World.bat” (no quotations). Now save it somewhere you can remember. The .bat (dot bat) at the end of the name tells windows… you guessed it… this file is a batch flie (.bat).

Testing it out

Go to where you saved it… and double click on the file if you did everything right it should look like image above and when you press a key it should close. (if not please reread and try again)

Congrats you just made a program using windows CMD interface.

Making it better

Right click on your program and click edit. Now it should open back up into notepad. When it does let’s add a bit more code to it…

Variables

Variables can make or break a Batch program, many are used to save numbers or information while the program is still running. *Note any variables set will be reset when the program is close* add this line right after one line below then code “pause”

Set var=hi
echo %var%
pause

What it does

Before we run it lets break it down, set: Tells the computer that a variable is about to be set, var: the name of variable that is being set, =: Make this…, hi:the value of the variable being set. Now for the execution, you know echo means say something, but what about %var%? The %% tells the computer “the value of” and var is the name so… what these two lines say is set the variable var and make it hi, then say the value of var which is hi.

Execute

In notepad click filesave. Start the program again but this time it will look like this…THE SAME??! But why? Press a key… that’s what pauses do, they stop everything until you press a key. It will now look more like this (below). Notice it still says press any key to continue, that’s the pause in action.

Furthering the Programs Advancement

Continuing with the subject of variables, now we will set a variable by asking someone what they want it to be. With that we need the /p command (P meaning prompt) delete everything in the program… add this
@echo off
Set /p var=what’s your name?:
echo hi %var%
pause

Break down

So what does all this does all this do? Well /p makes it (instead of just setting the variable) ask you what you want the variable to be while saying “What’s your name?:”

Run the program, when it asks type in your name type it and press Enter (or return) now it should look more like this

If yours looks like image above (maybe different name) you are ready to move on to the next section

GoToIf command

Some times you need to skip over a few lines of code in case a certain event happens such as a person enters a specific answer or you want to just skip a section. This is the goto command (along a lot with the IF command).

Put it in action

First make you program look like this

@echo off
Set /p var=what’s your name?:
IF %var%==no goto no
echo hi %var%
goto end
:no
echo :(
:end
pause

Break Down

Wow that looks like a lot, however its still a very straight-forward and basic. The IF says “if this happens do this” In this case If then variable var is equal to (yes you need both equal signs) no (you answer no) then go to segment no. The “:” says this is a segment so :no and :end are segments no and end. If you answer no then it will say :( and if you do answer that then it will say hi (answer given). (images below)

.

Chapter 2, Arithmetic and Putting it to use

Operations (if you’re running on Windows 98 or lower please skip this section)

Before I begin the next segment make sure you are familiar with the computer operation symbols (locations on the key board shown in the keyboard to left)
/ is division
* is multiplication
+ is addition
– is subtraction
= is equals
(shift)8 is also *

Putting that to use

Think of all the ways you use math in the world… now think of all the ways that it involves the 4 basic operations… almost all of them right. Well what math you can do the computer can do…maybe not better but, faster. Let’s get to the part you are reading this for, the coding.

@echo off
set number=1 (don’t add this but the number is name of the variable)
echo %number%+1
pause

Now run the program after it has ran continue reading it may not do what you expect

What happened?

Wait that’s not what its supposed to look like, you asked it to do 1+1 not say it… well you did and it took me a long time to find out what went wrong. In fact you are telling it to SAY the value of the variable number with a +1 behind it. To tell the computer to DO 1+1 we need a different approach.

Really putting it to use now!

To tell the computer to compute 1+1 we need to make sure it knows that that’s what it’s supposed to do! To accomplish this you need to type in the following code (deleting the old one).

@echo off
Set number=1
set /A number=%number%+1
echo %number%
pause

(Once Again) Break Down

You have been introduced to a new idea, the arithmetic statement (/A). By entering this it prepares the computer for an arithmetic equation (in this case %value of number% +1). By doing this you can effectively create a calculator (will be done later on). If you run the new program (remember to save it) you should get something like this

Beefing it up!

Lets make this simple 1+1 adder a little more useful (not the calculator yet)! How about we make it count to any number you want by incorporating IF statements and Goto commands. Change you program to look like this

@echo off
Set number=1
:start
set /A number=%number%+1
echo %number%
IF %number%==2000 goto end (replace 2000 with any number you want *don’t add this to code*)
goto start
:end
pause

RUN IT!

What it does (heh heh changed it up)

This is the end of you program (if you didn’t change the 2000) it should have counted rapidly to the number you gave it then stopped. This is a classic example of what an IF statement can do. This mini program does +1 to %number%, checks and sees if %number% is equal to 2000, if not it then repeats until it does.

Another addition

@echo off
Set number=1
:start
set /A number=%number%+1
echo %number%
IF %number%==2000 goto end
goto start
:end
set /A number=%number%-1 (yes that’s a minus sign)
echo %number%
IF %number%==0 goto start
goto end

Wow that’s a bit longer, but before you run it what do you notice, surly you notice that it is longer, but what else, now there is 2 IF statements, dividing the program into 2 different parts, and adder, and a “subtractor”. What it does is when the number reaches 2000 instead of stopping, it then goes down to 0 again. When it reaches 0 it will go back up to 2000! A never ending loop.

Your Largest program yet! (Well will be soon)

Well if your largest program is under the operations section and I’ve been hinting at a calculator then… YOU’RE MAKING A CALCULATOR. For now it will just add but PLEASE SAVE THIS WE WILL USE IT LATER!

@echo off
set /p num1=Please enter first number:
set /p num2=Please enter first number:
set/A num3=%num1%+%num2%
echo %num1% +%num2% = %num3%
pause

Don’t worry it gets more complex than that, all this does is add. Save it as Calculator.bat in a place you will remember for we will come back to this later to make it more complex.

Chapter 3, Choices

Simple Choices

OK let’s start a new notepad document. There is other ways of making choices… this time we will learn my way of doing them, the easy way. First let’s just make a simple yes or no answer.

@echo off
:start
echo Do you like programming?
set /p choice=(y/n) (The y/n prompts them of the 2 choices they can make)
If %choice%==y goto yes
if %choice%==n goto no
:yes
echo me too!
goto end
:no
echo WHY ARE YOU DOING IT THEN!?
goto end
:end
pause

Breakdown

Simple enough, it gives you a question and you have to answer it by saying yes or no (in this case y or n). However, what if someone instead of giving values y or n says like yes rather than y. Well its an easy fix if you look at what the program does…

  • Asks you the question
  • Gives you a chance to answer
  • Checks and sees if the value of your choice is y
  • If not checks and sees if the value of your choice is n
  • Executes based on the answer given

Well if an incorrect answer is given it will just go on saying “me too” Then pausing. You don’t want that though, do you? Well if you add a line right after “If %choice%==n goto no” telling the program to do something else if both values are wrong. Well that’s why I put the :start in the beginning just to do this! Add the following after the line saying “If %choice%==n goto no”

Echo that value is incorrect please try again
goto start

Now if they but a bad answer in it will tell them it is incorrect and ask the question again!

Bringing back the Dead

Though we were done with that calculator… NOPE, I warn you this is a very long code but still do not copy it from this article to notepad, I designed it so it won’t work only retyping it will work. I did that because you can remember stuff better if you do it yourself, not copy it form other sources. Well here it is a calculator that incorporates everything that we have learned in this article from the basic echo, down to the more complex choices. Once again don’t copy this into notepad.

@echo off
:start
set /p num1=Please set first number:
set /p num2=Please set second number:
:not_valid
echo 1 for addition
echo 2 for subtraction
echo 3 for division (rounded)
echo 4 for multiplication
echo 5 for new numbers
set /p operation=Please enter choice number and press enter:
If %operation%==1 goto addition
if %operation%==2 goto subtraction
If %operation%==3 goto division
If %operation%==4 goto multiplication
If %operation%==5 goto start
echo that wasn’t a valid choice please try again
goto not_valid
:addition
set /a num3=%num1%+%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:subtraction
set /a num3=%num1%-%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:division
set /a num3=%num1%/%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:multiplication
set /a num3=%num1%*%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:end

Now for the epic Break Down

Wow if you were loyal enough to type all of that then, you have what it takes to be a programmer! Since this is so long ill break it down into pieces

@echo off
:start
set /p num1=Please set first number:
set /p num2=Please set second number:
:not_valid
echo 1 for addition
echo 2 for subtraction
echo 3 for division (rounded)
echo 4 for multiplication
echo 5 for new numbers
set /p operation=Please enter choice number and press enter:

This part is basic @echo off and shows the start. It also asks you and sets the 2 numbers you want to be manipulated. It then tells you all the choices and asks you to pick one. Also gives a place if you put in a non valid answer for the next part

If %operation%==1 goto addition
if %operation%==2 goto subtraction
If %operation%==3 goto division
If %operation%==4 goto multiplication
If %operation%==5 goto start
echo that wasn’t a valid choice please try again
goto not_valid

Here the system is running though all the possible outcomes you could have put in and if it was invalid will send you back to try again

:addition
set /a num3=%num1%+%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end

Here it will manipulate your numbers for addition (if you chose)then give you your result and prompt you if you want to go again or quit

:subtraction
set /a num3=%num1%-%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:division
set /a num3=%num1%/%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end
:multiplication
set /a num3=%num1%*%num2%
echo %num3%
set /p again=Go Again (y/n)?:
If %again%==y goto start
If %again%==n goto end

Same for subtraction division, and multiplication

:end

This is here in case you chose to quit ( :( ) it will then close

1 last thing

Something I was going to mention was you can use Batch to open stuff such as web sites

@echo off
start www.triond.com

this will bring you to the triond website then close

Or programs

@echo off
start (now enter the program address {where it is}. First you have to make the folder names all 1 word by renaming them (DOS sucked).
@echo off
start C:[folder address][name of file]

Figuring out how that works is up to you, confuses me sometimes, and use of that is impractical, but if you get good at is, that is good practice


http://trifter.com/usa-canada/north-carolina/nc-hyco-lake-fishers-paradise-speed-demons-track/

http://gameolosophy.com/games/online/runescape/runescape-the-massive-multiplayer-online-world-guide/

http://gomestic.com/pets/keep-your-dog-held-down-can-make-him-run-off/

Well that’s it I hope you learn from this. Comment please

09.08.06

This was awesome thx.

Joel Fingerer
09.08.06

Awesome, thank you.
I added stuff to mine, like when it asks what’s your name and I type my name it says welcome you highness!

comments powered by Disqus
Loading