My First Batch File Program – Print Hello World

In the previous tutorial, I have given you the enough introduction to batch files, their modes of operation and the classification of commands used in batch files. If you missed it, please read it before you proceed to write your
first program.

Read: Introduction to Batch File Programming

 

How to Create a Batch File

First before writing the program, I’ll give you the basic procedure to create, and run the batch files.

1. Open any text editor you have. A normal notepad that is present in windows is enough. You can open notepad through the run. Just press “win+r” keys on your keyboard, in the run dialog box type “notepad” and press enter. This opens a new notepad.

2. Next write the commands of your batch file.

3. Press “ctrl+s” to save your file. Name your file with an extension as “.bat”, for example, “Hello.bat”. Then select all files and click on save.

4. Then open command prompt (typing “cmd” in run dialog box opens up the command prompt) and type your file name along with the extension to run it. You may also double click on your batch file to run it directly.

 

Print Hello World Using Batch Commands

Program version #1

echo "Hello World!!!"

This seems very simple. Your first program consists of only a single line. Isn’t it awesome? Yes, it is. This is the beauty of batch files.

The echo is used to display a message that is several lines long you can include several echo commands one after the other in your batch program. This is similar to printf() function in C, we need not use any quotes to specify the message here. Content that immediately follows the echo command is printed.

Copy the above line and paste it in a notepad and save it as “HelloWorld.bat” on your desktop. Now open up the command prompt, then navigate to desktop and type “HelloWorld.bat”. You will get the output as shown below.
 
My First Batch File Program - Print Hello World
 
We can clearly see that first the command is directly printed on the command prompt (called command-echoing) and then the command is executed to the output in the second line with quotes as I’ve given quotes around Hello World!!! in the program. To know why this happens, we need to closely inspect the echo command.
 

Syntax of echo Command

echo [{on|off}] [message]

In the above syntax, on|off specifies whether to turn the command-echoing feature on or off. The message specifies the text that you want to print on the screen.

To prevent echoing of a line on the screen, insert (@) at sign in the start of a batch program. So let us rewrite the
program using echo off.
 

Program Version #2

@echo off
echo "Hello World!!!"

Output
C:Usersuser1Desktop>HelloWorld.bat
“Hello World!!!”

So, we have removed the command-echoing successfully. Remember that I’ve said that we can directly double click the batch file to execute it. But, in this case, you can double click it and see that the command prompt closes immediately after printing the message. To stop it from closing we need to use a built-in command, that is the “pause”.
 

Final Version of Hello World

@echo off
echo "Hello World!!!"
pause
Output
C:Usersuser1Desktop>HelloWorld.bat
“Hello World!!!”
Press any key to continue . . .
So, we see that when we use pause command, the command prompt stops and asks the user to press any key to continue its next task. In this case as there are no other commands, the prompt closes immediately after
pressing a key.
 

Try Out yourself

Try out these simple programs and see the outputs on your own.

Program 1

echo off
echo. 
echo formats and checks 
echo new disks 
echo This batch program 
echo.

Program 2

@echo off
echo ^<Less than,^> Greater than, ^|or Symbols

4 thoughts on “My First Batch File Program – Print Hello World”

  1. when going to CMD to execute the batch, users may have to change directories to where they have saved the batch file in order to execute

Leave a Comment

Your email address will not be published. Required fields are marked *