Introduction: Batch Files: Some Basics.....
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.
27 Comments
8 years ago on Introduction
I don't think we ever defined "playagain" or did I miss that? Great tutorial!
12 years ago on Introduction
i made the bat file's but there is a error dunno i've done everything right......
14 years ago on Step 7
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
Reply 14 years ago on Step 7
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.
Reply 14 years ago on Step 7
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.
14 years ago on Introduction
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???
Reply 14 years ago on Introduction
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.
Reply 14 years ago on Introduction
Ok, so to start the shutdown sequence i have:<br/><br/>IF %GuessNum% EQU 4 GOTO shutdownG<br/><br/>under :Retry. For the whole :shutdownG i have:<br/><br/>:shutdownG<br/>SHUTDOWN -s -f -t 60 -c "Keep guessing! If you don't guess the right number, the computer will shut down!"<br/>:shutdownG1<br/>SET /p Guess=<br/>IF %Guess% LSS %Answer% ECHO My Number is Higher.<br/>IF %Guess% GTR %Answer% ECHO My Number is Lower.<br/>IF %Guess%==%Answer% GOTO END<br/>ECHO.<br/>SET /a GuessNum=%GuessNum%+1<br/>GOTO shutdownG1<br/><br/>and then in :end i have:<br/><br/>IF %GuessNum% GTR 4 SHUTDOWN -a<br/><br/>Is there something wrong with any of these, or could the problem be elsewhere?<br/>
Reply 14 years ago on Introduction
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
Reply 14 years ago on Introduction
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.
14 years ago on Step 1
Thanks for explaining! I hadn't realized yet.
15 years ago on Introduction
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!
15 years ago on Introduction
The %random% variable returns a random value between 0 and 32,767.
15 years ago on Introduction
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
16 years ago on Introduction
is this a virus?
Reply 16 years ago on Introduction
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.
Reply 16 years ago on Introduction
ok but i dont think ill try it cuz um i wont.
Reply 16 years ago on Introduction
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.
Reply 15 years ago on Introduction
dude youre totally right its not a virus...its just a really addicting little number game
Reply 15 years ago on Introduction
Of course I'm right! I wrote it didn't I? Glad you enjoyed it. :)