loading
This Instructable will teach you some basics of writing batch files, and will show you how to create a number guessing game with a couple twists, just to keep things interesting...

I've learned most of my command prompt skills, and all of my batch writing from the internet in general, and Instuctables in particular. I'd like to give a thank you to Instructables user Neodudeman for his great Instructables on batch writing. Thanks!

Step 1: What Is a Batch File?

Well, most of my readers probably already know what a batch file is, but just in case....

A batch file is a simply a collection (batch) of MSDOS commands that execute sequentially when you run the batch file. Batch files start as .txt files in notepad, and become executable files when when you save them as something with a .bat extension. So basically, all you do is write a file in Notepad, and then save it as, say, "instructable.bat". Once the .bat is placed at the end of the file name, a nice, new file will apear, named whatever you named it, with a nice, gear looking icon.

Okay, now that we know what these batch files are, lets get to writing!

Step 2: Basic Commands

Allright, you're going to need to learn a few commands. So first, open up the MSDOS command prompt. Open the start window, click on run, type "cmd.exe" and then click run.

Okay. First, we're going to look at variables. Variables are numbers, words, or other things that, (somewhat obviously), vary. The Command Prompt has a variable funtion. It has some variables that are already set, such as TIME, DATE, and a few others. Most variables, however, you can set yourself.
Click on the Command Prompt window, and type:

SET variable=random

It really doesnt matter if you capitalize the "SET" or not, but I like to capitalize all of my commands, especially when writing batch files. It just makes it easier to tell what you're doing.

Now hit enter. Good for you! You've set your first variable! But what can we do with it? who cares if all we can do is set the variable right? Well, as it turns out, we can do a lot more than that, but first, lets just try to get the computer to tell us what the variable is set as. Okay, in order to get the computer to read the value of a varible, we type the name of the variable, in this case, "variable" and put the name inside % marks, like this: %variable%. Go ahead and type that in, and hit enter:

%variable%

Weird error huh? The computer said "'variable' is not recognized as an internal or external command, operable program, or batch file." So why did it give that error? Well, basically, it gave that error because the Command Prompt treats the value of that variable as if you typed it in yourself. So when you typed %variable%, the computer thought you were telling it to execute the command "random". Obviously, we need something else in order to see the value of our variable. This is where the ECHO command comes in.
The ECHO command simply tells the Command Prompt to echo, or say, whatever you typed after ECHO. So, if we type the ECHO command before our variable, we should get what we want:

ECHO %variable%

There! Now we've got what we want! The computer prints "random". Obviously, this was the value we typed for our variable, so it's the result we wanted. In the next step, we'll learn more about variables, and how we can use them.

Step 3: Using the SET Command and Variables

Okay, now that we know what variables are, what else can we do with them? Well, we can do math, we can use them as conditions for writing programs and batch files, we can perform basic arithmetic, we can execute commands, and so much more. We won't go into everything you can do with variables, but we will discuss some important aspects of the variable funtion.
First, the SET command by itself only produces string variables. This means that it won't add or do any other math. If you were to tell the computer to add 1 to a variable with a value of 1, it would give you the value 1+1. If we want it to actually add the two numbers, we need to place a "/a" after the SET command. Therefor, we type:

SET /a varible=(value)

Now, suppose we want to put a variable in our batch file that the user will provide. We might want to do this if we were computing values according to a formula, or, in our case, if we want the user to guess a number that the computer has come up with. To generat a user specified variable, we add a /p after the SET command, and we leave the area after = blank:

SET /p variable=

There you go! A user specified variable! If we were to put this line in a batch file, then the batch would run until it reached this line, and then it would wait for user input before continuing. The other cool thing about the /p is that it completely negates the /a. When we include a /p we can just omit the /a.
The next thing we'll learn about it producing random variables. If we want the computer to pick a random number for a variable, we simply type the SET command, followed by the variable, and then set the variable to equal %RANDOM%. Once again, it doesn't need to be capital, but I like to do it anyway. So, we type:

SET /a variable=%RANDOM%

Obviously, this isn't a user specified variable, so we include the /a. Cool! So now we know how to produce a random number! But how random is it? Well, it turns out that the computer picks a number between 0 and somewhere around 37,000. I'm not sure what the exact number is. But what if we want a smaller number? Suppose, as in this Instructable, we want a manageable number for something like a guessing game? Well, that's where the IF command comes in....

Step 4: The IF and GOTO Commands. Power in the Hands of the Batch Writer.

So, we want to produce a manageable number. Lets suppose we want to produce a random number between 1 and 20. Okay, that's easy to say, but the RANDOM value chooses a number between 1 and 37,000. That's what we're going to use IF for. The IF command basically says IF something happens, or IF something equals, or does not equal, a certain value, THEN do THIS. So, IF sets conditional commands. We want to produce a number that is less than twenty, but greater than one, obviously, we'll start out with telling the computer to pick a random number, but then we'll need to be able to tell it to pick a new number if the number it picks doesn't fit our requirments. That is where the GOTO command comes in. GOTO simply tells the computer to GO TO a certain label in the code. Labels look like this:

:pick

Any word placed after a colon becomes a label that we can access with the GOTO command. So, if we want to go to the section of code with the "pick" label above it, we simply type:

GOTO pick

Alright, so lets continue with our coding. We have already told the computer to pick a random number, so we've typed:

SET /a answer=%RANDOM%

Now we want to pull this number down to a smaller range. So we'll invoke the IF command. Something like this should do the trick:

IF %answer% GTR 20 GOTO pick

This tells the computer to GOTO pick IF the answer is GReaTer than 20. We could also put any of these conditions on that IF command:

EQU - Equal
NEQ - Not Equal
LSS - Less Than
GTR - Greater Than
LEQ - Less Than or Equal To
GEQ - Greater Than or Equal To

Thus, with IF, GOTO, labels, and these abreviations, we can manipulate our batch file any way we choose. Okay, so we've got our random number under twenty now, and here's what we've got so far:

:pick
SET /a answer=%RANDOM%
IF %answer% GTR 20 GOTO pick

Now, lets make sure that the computer doesn't pick 0 for the answer.

:pick
SET /a answer=%RANDOM%
IF %answer% GTR 20 GOTO pick
IF $answer% EQU 0 GOTO pick

Okay! Now we've got a usable number between 1 and 20. Lets move on to the meat of the Batch.

Step 5: The Meat of Our Game

Allright, we've got our random number. We also want to count how many guesses our player makes, so we'll set another variable:

SET /a guessnum=0

That sets the geussnum variable to zero, and we gave it the /a parameter, so we'll be able to add to it every time the user guesses.

Okay. We've got a random number, and we've set the number of guesses. Now we need some instructions, and we need to have some user input for the guess number. You should understand most of this by now, so I'll just show you the code:

:begin
ECHO I'm going to think of a number
ECHO I'm thinking.....
SET /a GuessNum=0

(This ECHOs those two lines and sets the number of guesses to 0)

:pickA
SET /a Answer=%RANDOM%
IF %Answer% GTR 20 GOTO pickA
IF %Answer% EQU 0 GOTO pickA
ECHO I'm thinking of a number between 1 and 20
ECHO Guess what Number I'm thinking of.

(This section loops until it SETs our random number, and then ECHOs the instructions for our player)

:Retry
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
GOTO Retry

(This section tells the computer to ask for user input, and then loops continuously until the user picks the correct number. Then, it GOes TO the label END)

:END
ECHO You are Correct! The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO.
PAUSE
CLS
ECHO Would you like to play again?
ECHO Y/N?
SET /p play=
IF %play% EQU y GOTO begin
IF %play% EQU n GOTO close
IF %play% GTR y GOTO playagain
IF %play% LSS y GOTO playagain
IF %play% GTR n GOTO playagain
IF %play% LSS n GOTO playagain

(Here's our end section. This tells the user how many guesses they took, and then asks if they would like to play again. Notice that we can use the EQU, GTR, and LSS with letters too.)

Okay! If you simply copied this code, you would have a legitmate guessing game. Not real fancy, but hey, it's better than most people can do. But we're going to add a little twist, just to make things interesting....

Step 6: The Twist

Now, we do have a working game right now, but we want to make it a bit more intersting. How about adding some incentive for our player to guess the right number? How about we do somethig like.. shutdown their computer if they don't guess the number? That would be pretty cool! Okay, now we'll add a little code to make these changes. First, we're going to add a line to the section of code we labeled "retry". So go find that section. It looks like this:

:Retry
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
GOTO Retry

Okay, we're going to add this line right after the "ECHO." (When we put a period after ECHO, it leaves a blank line.) Here's the new code:

IF %GuessNum% EQU 4 GOTO shutdownG

When we add this line, the section looks like this:

:Retry
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
IF %GuessNum% EQU 4 GOTO shutdownG
GOTO Retry

By now, it should be pretty obvious what this does. It tells the computer that if GuessNum EQUals 4, it should go to the section of code labeled "shutdownG". So, what do we want this shutdown section to say? Well, obviously, it has to be labeled "shutdownG". Next, it has to shut the computer down. The command to shutdown is "SHUTDOWN -s". This will shut the computer down, but we want to add some to the command. We'll add a "-f". That will Force all programs to close, and we'll add a "-t 60". That will tell the computer to display a window and wait sixty seconds to close. We'll also add "-c "message here"". which will display a message in the shutdown window. After our shutdown command, we'll tack on the same code we have above, the code that allows our player to pick numbers, and gives them feedback.
So our shutdown code now looks like this:

:shutdownG
SHUTDOWN -s -f -t 60 -c "Keep guessing! If you don't guess the right number, the computer will shut down!"
:shutdownG1
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
GOTO shutdownG1

Now we have set the computer to SHUTDOWN, and display a message, but we also need to tell the computer to stop the shutdown, if it has been initiated. So, we'll add that to the section of coding labeled "end". That section looks like this:

:END
IF %GuessNum% GTR 4 SHUTDOWN -a
ECHO You are Correct! The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO.
PAUSE
CLS
ECHO Would you like to play again?
ECHO Y/N?
SET /p play=
IF %play% EQU y GOTO begin
IF %play% EQU n GOTO close
IF %play% GTR y GOTO playagain
IF %play% LSS y GOTO playagain
IF %play% GTR n GOTO playagain
IF %play% LSS n GOTO playagain

We want to stop the shutdown, and we do that with the "SHUTDOWN -a" command. So, we'll add a line that goes like this:

IF %GuessNum% GTR 4 SHUTDOWN -a

We'll add that command right after the label, and that will tell the computer to run the SHUTDOWN -a command only if the player has made more than four guesses, and started a shutdown. Okay! you should have your game finished now! WE'll make sure there aren't any bugs in the next step.

Step 7: Final Steps

Okay, now if you strung together all that coding, then you'll have something that looks something like this:

:begin
ECHO I'm going to think of a number
ECHO I'm thinking.....
SET /a GuessNum=0

:pickA
SET /a Answer=%RANDOM%
IF %Answer% GTR 20 GOTO pickA
IF %Answer% EQU 0 GOTO pickA
ECHO I'm thinking of a number between 1 and 20
ECHO Guess what Number I'm thinking of.

:Retry
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
IF %GuessNum% EQU 4 GOTO shutdownG
GOTO Retry

:END
IF %GuessNum% GTR 4 SHUTDOWN -a
ECHO You are Correct! The Answer was %Answer%
ECHO It took %GuessNum% Guesses.
ECHO.
PAUSE
CLS
ECHO Would you like to play again?
ECHO Y/N?
SET /p play=
IF %play% EQU y GOTO begin
IF %play% EQU n GOTO close
IF %play% GTR y GOTO playagain
IF %play% LSS y GOTO playagain
IF %play% GTR n GOTO playagain
IF %play% LSS n GOTO playagain

:close
ECHO Thank you for playing!
PAUSE
EXIT cmd

:shutdownG
SHUTDOWN -s -f -t 60 -c "Keep guessing! If you don't guess the right number, the computer will shut down!"
:shutdownG1
SET /p Guess=
IF %Guess% LSS %Answer% ECHO My Number is Higher.
IF %Guess% GTR %Answer% ECHO My Number is Lower.
IF %Guess%==%Answer% GOTO END
ECHO.
SET /a GuessNum=%GuessNum%+1
GOTO shutdownG1

That should be everything we need right? So, go ahead and save that notepad .txt file you have as GuessGame.bat. Actually, you can name it whatever you want, as long as you put the .bat at the end. Okay, so click on the icon and run the program! Did it work? Well sorta. It's doing some weird stuff isn't it? Turns out that when we write a Batch like this, the command prompt ECHOs every command we give it, just as if we typed them into the command prompt. So the game works, but it's a bit messy and unclear. Can we do anything about this? Yep! All we have to do is type this line at the very beggining of our code:

@ECHO OFF

This tells the computer to turn the ECHO OFF. And the @ sign at the begging tells it to turn the ECHO OFF for every command. If we left that @ out, then it would only turn the ECHO OFF for one command.

Step 8: All Done!

Congratulations! You've just written a Batch file game. Pretty simple isn't it? If you can handle this, then you can figure out how do do quite a bit with Batch files. Just play with it, do some experiments. Just in case you couldn't get something to work, or in case I left something out in all that coding, I'll give you the file here.
<p>Awesome tutorial. Thnx...!!!</p>
<p>I don't think we ever defined &quot;playagain&quot; or did I miss that? Great tutorial!</p>
i made the bat file's but there is a error dunno i've done everything right......
at the begging u should have ECHO off :begin ECHO im going to think of a number if u dont have the ECHO off part all the comands will be displayed instead of just the ones u want
Thanks for the advice, but actually, if you simply said "ECHO off " it would only turn the echo off for the next command, not for all of them. The correct command to turn the echos off is @ECHO off. That will turn the echo off for the entire batch, not just one command. Also, if you look closely at the last paragraph of step 7, you'll see that I already addressed that problem, and explained it quite thoroughly. I just left it till then so that the reader could learn what the command does through experience.
Also, leaving the ECHO on is a great way to check the code for bugs, since it'll stop when it has a problem, and you can see the last thing it tried to execute.
Ok, something is wrong. If I play the game as soon as I write in the code that's supposed the shutdown, that code will work. However, when I choose to play again, or if I close the program then open it back up, the code doesn't work and the shutdown continues even when I get the answer right. What am I doing wrong???
Well, if you are trying to play it right after the step where you put in the shutdown code, then you haven't yet built in the abort section, and it will shutdown regardless. That's the only thing I can think without seeing what you've actually written.
Ok, so to start the shutdown sequence i have:&lt;br/&gt;&lt;br/&gt;IF %GuessNum% EQU 4 GOTO shutdownG&lt;br/&gt;&lt;br/&gt;under :Retry. For the whole :shutdownG i have:&lt;br/&gt;&lt;br/&gt;:shutdownG&lt;br/&gt;SHUTDOWN -s -f -t 60 -c &quot;Keep guessing! If you don't guess the right number, the computer will shut down!&quot;&lt;br/&gt;:shutdownG1&lt;br/&gt;SET /p Guess=&lt;br/&gt;IF %Guess% LSS %Answer% ECHO My Number is Higher.&lt;br/&gt;IF %Guess% GTR %Answer% ECHO My Number is Lower.&lt;br/&gt;IF %Guess%==%Answer% GOTO END&lt;br/&gt;ECHO.&lt;br/&gt;SET /a GuessNum=%GuessNum%+1&lt;br/&gt;GOTO shutdownG1&lt;br/&gt;&lt;br/&gt;and then in :end i have:&lt;br/&gt;&lt;br/&gt;IF %GuessNum% GTR 4 SHUTDOWN -a&lt;br/&gt;&lt;br/&gt;Is there something wrong with any of these, or could the problem be elsewhere?&lt;br/&gt;<br/>
Nothing looks wrong with that, but there has to be an error somewhere. I would suggest carefully looking over your code and comparing it to mine. It's probably something incredibly obvious that you're just skipping. I have those moments a lot. The other thing you can do is turn the ECHO on, so the computer will tell you what code it is executing, and then watch it execute. Then when it stops, or tells you it can't execute something, or skips something, you'll be able to see where the problem is. Hope that fixes it, let me know if you need more help. JC
Yer i agree with JC it would be a lot easer if you just showed us what you've done so far like a bit of your code with the shutdown bit in.
Thanks for explaining! I hadn't realized yet.
thanks for the tut! i love the way it's laid out! you got me started making basic batch files! thank you! i love it! make another!
The %random% variable returns a random value between 0 and 32,767.
um cool instructable but forgot something, some computers hide extentions(.bat/.txt/.exe) to fix this go into windows explorer(start/programs/accesssorys/ widow explorer) go to tools/folder options go to the view tab and UNTICK "hide exteantoins for known file types" then click apply p.scool instructable and srorry for selling
is this a virus?
No. It isn't a virus in any way. It won't damage your computer, install itself, replicate itself, or do anything else that viruses tend to do. It's just a game, written in MSDOS.
ok but i dont think ill try it cuz um i wont.
That's fine with me. But seriously, you don't need to worry about viruses, if that's why you don't want to try it. Anyone with basic knowledge of batch files can see that it's not a virus.
dude youre totally right its not a virus...its just a really addicting little number game
Of course I'm right! I wrote it didn't I? Glad you enjoyed it. :)
ITS A VIRUS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I don't know why you call it a virus in one breath, and say that it's cool and I should do another one in the next. It isn't a virus, I've already explained that it isn't a virus. But thanks for the nice comment. I think it's cool too. I might do another one if I get the time.
cool!!! please do another one!
I wrote the code like you said but how do I input my guess when plying? cause when I hit enter it closes? any ideas?
You should be able to just type the number in and then hit enter. I just copied my code and tested it, so I'm pretty sure that I don't have any errors. You might try looking through the code and checking to make sure that you don't have any errors. However, if you hit enter without typing a number, the window will close. So if you're hitting enter first, then that's whats going on. Type a number, and THEN hit "enter".
This is my first instructable, and I tried to do a good job with it, but cut me some slack if it's not world-class. Anywho, let me know what you think!
Seems like a good instructable to me -- I "faved" it.
Thanks!
Cool, but would not it had be faster to write this in c?
Well, I don't really know anything about C, but it probably would be. I just think the MSDOS commands and batch files are cool. Plus, they have a lot of good uses, if you need to fix or troubleshoot something on your computer.

About This Instructable

9,531views

26favorites

License:

More by JC The Sniper:Batch Files: Some basics..... 
Add instructable to: