Batch Files: Some Basics.....

by JC The Sniper in Circuits > Software

11338 Views, 27 Favorites, 0 Comments

Batch Files: Some Basics.....

C:\Documents and Settings\Owner.Jimmie\Desktop\batch writing fun.bmp
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!

What Is a Batch File?

C:\Documents and Settings\Owner.Jimmie\Desktop\.bat examples.bmp
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!

Basic Commands

C:\Documents and Settings\Owner.Jimmie\Desktop\variable error.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\echo variable.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\set variable.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\%variable%.bmp
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.

Using the SET Command and Variables

C:\Documents and Settings\Owner.Jimmie\Desktop\user set vari.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\variable string.bmp
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....

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

C:\Documents and Settings\Owner.Jimmie\Desktop\pick loop.bmp
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.

The Meat of Our Game

C:\Documents and Settings\Owner.Jimmie\Desktop\notepad text.bmp
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....

The Twist

C:\Documents and Settings\Owner.Jimmie\Desktop\shutdown code loop.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\shutdown code1.bmp
C:\Documents and Settings\Owner.Jimmie\Desktop\shutdown abort.bmp
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.

Final Steps

C:\Documents and Settings\Owner.Jimmie\Desktop\@ECHO off.bmp
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.

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.