Batch file in windows

What is a batch file
A batch file is a file that you can create in windows to perform some tasks. This file contains windows commands and can be used to automate manual actions.


Example,
suppose you have a folder that contains some junk files and after every couple of days you need to clean those files.
One way is to select all the files that need to be removed one by one and then delete them by pressing delete key. Sounds boring, isn’t it?
Another much simpler way is to create a batch file with commands to select those files and remove them. Now every time the files need to be removed, just execute the batch file and you are done!!!
Batch file extension
A batch file has a .bat extension and has a gear like icon as shown below.
batch file iconHow to create batch file in windows
Creating a batch file on windows is very easy. You do not need any special tool for that. Just open a notepad, type windows commands that you want to execute in this batch file and save this file with a .bat extension as shown below.
saving batch file
A batch file can contain any windows command that could be executed normally at the command prompt such as

  • cls – To clear the screen.
  • copy – To copy files from one location to another.
  • ping – To ping a URL and check if it is responding.
  • ipconfig – Details of system ip and addresses.
  • cd – Change or navigate to a folder.
  • dir – List all the files in the current directory.
  • mkdir – To create a directory.
  • del – To delete a file.

Remember that the commands written in the batch file are executed from top to bottom in the same order in which they are written.
Writing a batch file example
Below is a batch file containing command to print a message on the screen. Save this file as test.bat

echo OFF
echo Learning to write batch file
pause

where,
Line 1: echo OFF prevents the command from being written to the screen before execution. Without this, first the command will be written to the screen and then executed.
Line 2: echo command is used to print a message to the screen. Anything that follows echo is printed to the screen.
Line 3: pause is used to halt the execution of batch file. When pause is encountered, the execution stops till a key is pressed.
Note that the commands in a batch file are not case-sensitive. You can write echo, Echo, ECHO etc., and all will work.
How to run a batch file
There are two ways to run a batch file as explained below.
1. Run batch file in command prompt
Open windows command prompt and navigate to the location where it is saved. After the command prompt is in the location of batch folder, just type the name of the batch file and hit enter, the batch file will execute. Example,

E: \> cd batch
E: \batch>test.bat
E: \batch>echo off
Learning to write batch file
Press any key to continue . . .

2. Run batch file by double clicking
Another method of executing a batch file is by double clicking it. When a batch file is double clicked, it will open a new window which looks similar to the command prompt. Output of the batch file will appear in this window. Example,

E:\ batch>echo off
Learning batch file
Press any key to continue . . .

With this approach you must use pause command at the end otherwise the window will automatically disappear after the execution completes.
A simple problem with this approach is that if there is any error in batch file execution, then you may not be able to see it since the batch file might close in case of an error.
Further, after execution, this window will disappear on the press of any key where as in first method, the execution window remains there.
Editing batch files
A batch file once created can also be modified. For changing the contents of a batch file, open it just like any other text file, change the contents and save.
Right click the batch file and Select Edit option. This will open up the batch file in notepad where it can be easily modified.
Comments in batch file
Comments are the lines or commands which will not be executed. Comments are also required to write some information about a command such as why it is written, what it will do etc.
Comments in a batch file can be written in two ways:
1. Using ::
Precede the line to be commented by a ::(double colon). Example,

:: This command will list the ip details of the system
ipconfig
pause

2. Using REM
REM keyword can also be used to comment a line. Precede the line to be commented using REM and it will not be executed. Example,

REM This command will ping google.com and check if it is up and running
ping google.com
pause

In order to comment multiple lines at once, select all the lines and press Ctrl-Q keys. Follow the same procedure to uncomment those lines.
Hope by going through this article, you learnt about creating and running batch files in windows. Click the clap below to encourage more such articles.

Leave a Reply