Batch File – What is it? How to Create, Edit & Run?

What is a Batch File?

A batch file is just a text file (with .bat extension) that contains a sequence of commands for a system. When you run a batch file, the commands written in it are executed in the Command prompt serially. A batch file does the work of a mediator between user & the command prompt by executing commands serially which otherwise would have to be entered manually line by line. Hence it plays a significant role in automating windows tasks. A batch file minimizes user intervention. For example: A user can schedule the OS to restart after a specific amount of time.

How to create a Batch File?

Let’s create a demo batch file named MyFirstBatch that would just display some statements on the command prompt.

Step 1: Open a new text file (launch notepad)

Step 2: Type the below commands:

echo off

rem The below statements should be displayed on screen

echo Executing MyFirstBatch.bat

echo Congratulations! The batch file has been successfully executed.

Step 3: Save the text file as “MyFirstBatch.bat

Save As dialog box

The saved batch file appears as this:

How to run a Batch File?

The batch file can be run in two ways:

1. Double click on the batch file:

Go to the folder where the batch file has been saved. Just double click on it.

Also, you can right click on the batch file & Select Run As Administrator.

2. Run from command prompt:

Go to the folder where the batch file has been saved. Launch Command prompt from there.

Browse for the file in the command prompt & press enter. Now your batch file should run.

How to edit a Batch File?

When you run “MyFirstBatch.bat”, Do you just get a glimpse of something appearing & then going away?

Not to worry. You need a small modification in your batch file. Right click on your batch file & Select edit. Once your batch file opens, Type “pause” at the end. Now your batch file has the below commands.

On running the batch file now, the output is displayed as below:

Batch Commands Used:

Echo: Displays text on the screen.

Pause: Halts the execution of the batch file. Waits for the user to press a key.

Rem: Helps to include comments / remarks in a batch file








Leave a Reply

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