Saturday 14 September 2013

Windows Batch File - Create, Edit & Execute

The Windows Batch File is simple text file that contains a sequence, or batch of commands. It is mainly useful for storing sets of commands to run one after the other to accomplish a repeated task.

Instead of entering a command and executing it manually every time,
you can easily give the batch file name to execute those commands automatically on command prompt. You can also put few conditions and execute the commands needed.

In DOS (DOS - Disk Operating System) systems the batch files end with .bat or .BAT extension.

The Best known DOS file is AUTOEXE.BAT which initializes DOS when you start the system.

Step 1 : Create a .BAT file

   
Create a new text document. Double click the file once the file opens select File>save as, and in the save as window, provide you sample name for the batch file and then add the extension .BAT or .bat on the end.
 Which ends up creating sample.bat file.

Note: Also you need to check that windows doesn't stick to the standard .txt format as extension on the end of this sample file.
 To check this change the file type from .txt to "all files" as shown in the picture below.


Step 2 : Edit the batch file - Also learn few simple commands here

If you know how to run commands in command prompt then it is very easy.
Here in the batch file what we do is put all the commands in the file and execute them automatically one after the other, rather than typing it one by one manually and wait for the result.

Let's learn few basic batch commands which will be helpful. Here the advantage it is not case Sensitive.
Title : The window name for the batch file.

Echo :  The print statement for the batch files. Anything followed by the word ECHO will be displayed in the command prompt as text. 

Echo OFF : This is typically used by all the batch programmers at the beginning of their files.  It actually means that the program won’t show the command that you told it to run while it’s running – it will ll just display the command. I would recommend that after you run this sample program, you try removing this line from your code to see what's the magic of this.

PAUSE: This outputs the "press any key to continue" message.  This is mainly used to make human intervention to continue running the batch file after "Enter" is pressed.

CLS : Clears the DOS Window screen.

PING: Pings an IP, with this we can be able to check whether we are able to connect to it or not. It returns the time taken to ping and by default it tries to ping itself for 3 times.

Here is the sample.bat file code.


Step 3 : Execute the batch file 

     
Go to Start menu and open All Programs > Accessories > Command Prompt or go to run and enter cmd and press enter to open command prompt.
By default Local disk where the operating system installed will by default.
If you type echo "Learner Adda", then it will be printed.

Go to the directory where the batch file is placed and execute the script in the following way.
C:\> cd learneradda
C:\learneradda\> sample.bat

Then the sample.bat batch file will be executed and result will be printed. Here is the example batch script for your reference.