Batch files are nothing but a text file containing some command lines. Each command lines will be in different lines so that they execute one by one. What are these Command lines ? in MS-DOS Command lines are basically a strip of code, which could be like a line with the command Name followed by any required parameter. At the time of  text-based operating systems, they used the Computer by typing those command lines, ie If we wanted to copy a file there the only way was to use Command lines.

When Windows was released as a GUI-Based operating system, they built the OS to run on MS-DOS. Hence by studying Batch file programming you may be able to do many things which are not provided in the GUI. Learning Bat file programming is nothing but learning Command lines used in it , and the syntax to use them. In this tutorial, I will be teaching you some of those command lines. Also how to create a Bat file program. There are three extension for such a file which are .bat , .cmd and .btm . These files may have different extensions, but they work the same.


Create A Batch File

  • A batch file can be created by typing the command lines in a notepad (txt file) , then change its extension to .bat

rename


or, You may open a new Notepad window, paste all command lines and then choose Save as option from the File tab and name it as Filename.bat 

  • After writing a Command, next command should be in the next line.
  • Spaces should be properly provided.
  • Variables are not case sensitive.
  • No need to specify the variable datatype.
  • You may practice commands using the Command prompt.

Let Us Start

  • I will start by creating a Batch file which will print Hello World !
  • First of all create a Text file (We could rename it later).

Echo Command

This Command is used to display our output, or simply Print anything. Means a command line that you should know.

eg: echo Teczoz

Output: Teczoz

To start a new line the command used it ” echo.”   (echo followed by a dot)

We may Use     @echo off     command which is really useful in a program. It just hides the extra unwanted outputs/texts like Working Directory. And it helps us to keep the beauty of the program. So I recommend you to use this command line at the very first of every Batch program.

Now we got the commands with which we could proceed to create our first Batch file program. The program is given below

@echo off

echo Hello World !

Fist Program

Now rename the text extension to .bat, Now just double click on it to get the output.

Output: Hello World !

output

Congrats You wrote your first Batch File program. That was simple right? Now there is a command line with which you could tell the program to show the output till the press of any key. The command id Pause. It will wait for us to press any key and also prints “Press any key to continue . . .”If you don’t need this command line to print anything you may use Pause >nul. Now this command will wait you to press any key, but it will not print anything.

Now let us add pause command to our program. Just right click on the file and click edit. After change the program as follows.

@echo off

echo Hello World !

pause

Pause

Output :

Hello World !

Press any key to continue . . .

Pause Output

The exit command is another basic command that may be needed in the program. We may use it in such situations when we need to close the program inside a loop after satisfying a condition.

1 2 3