Introduction: Batch Programming: Greeting a User by Name + Other Programs

If you are completely new to Batch, I strongly suggest that you visit the first tutorial in Batch Programming before proceeding with this one.

If you are new to Batch but have already done the first tutorial, I also strongly suggest that you do these programs in order because certain commands will only be taught once.

Step 1: Greeting a User by Name

1. Open up Notepad or the desired text editor.

2. Type this into Notepad:

@echo off

echo What is your name?

set /p name=

echo Welcome, %name%!

echo.

pause

You are now asking: What do these new commands mean? Well, I'll tell you.

set /p name=: This allows the user to type something in the batch window and then stores the user's input in a variable after the user presses Enter. In between the set /p and the =, you type in the name of the variable (which in this case is "name"). If you want to bring it up sometime again for the user to see, just type in echo followed by %nameofvariable% . That's why we did "echo Welcome, %name%!" in the third line of this program.

pause: This command waits until the user presses Enter or another key.

echo.: This creates a new line in the output of the program to space out commands. (usually placed in between two "echo"'s.)

7. Save it as a batch file and run it!

Step 2: Changing the Color of a Batch Window

Now I will teach you about the colors you can add to a Batch window.

To do this you will type this into Notepad:

@echo off

color 0a

echo This is a color test.

pause

The default in Batch is white text on black background. To get a different look, you will need to type in the color command. Here's how it works:

The first parameter of color is the color of the background. What you just typed was 0. That means the background is the default black. The second parameter is the color of the foreground (the text). You typed "a". So, your text is now green. Save that code as a batch file. If you do not know how to do this, you can download the informational text file attached. When you open the batch file, you should see something like the screenshot at the top of this step. Now close the Batch file, right click on its icon, and click on "Edit" to edit it in Notepad. Now change the color command's parameters as you please. Just don't tinker with the other commands unless you know Batch well because it is a powerful language. The possible parameters for the color command are in the picture above.

Attachments