A batch file is an unformatted text file or script file which contains multiple commands to achieve a certain task. It contains series of command that is executed by command line interpreter.
Extensions: .bat or .cmd
The instructions in batch files are for automating repetitive command sequences.
Before the implementation of modern GUI’s ( Graphical User Interface ), in the operating system like MS-DOS, we had to operate every command from command line. Even though we are facilitated with GUI’s, many major core operations can only be achieved through command line instructions.
So whenever we write instructions or codes in batch files, we are executing command line operations through our instructions and when we know how to write commands, we can do many powerful things in the Windows.
For example: We can create a .bat file with instructions of shutting down and whenever clicked in that file, Windows will automatically shut down.
Sounds fun, right?
How to create a batch file?
Creating a Batch file is very easy, open any text editor (notepad.exe) then save that file (Save as...) with an extension .bat.
If you want to run that file you need to double-click on it or call it by the command prompt.
Well as simple as it sounds, you don’t need any extra software installed to create a batch file.
Voila, you have created your first ever batch file! 😎
But you haven’t put any instructions...yet.
Let's create the first script!
Let’s create a simple batch script to display “Hello World!”.
By default, when you run a Batch file it displays every command it’s currently executing.
In order to turn off this display you have to use command: @echo off
Then, we will write a line of command that will display "Hello World!":
echo Hello World!
And out last line of code will be:
Whole code should look like this:
Note: If you didn’t add PAUSE to the file, the batch file would simply run its commands and then automatically close.
When you want to quickly run commands without seeing the output, you can omit this line of code (Pause)
If you’re running several commands, you could place the PAUSE command in between them.
How to run a batch file?
It may sound funny, but all you have to do is click that file to run it and Windows will automatically run the commands written in a batch file.
A batch file can also be run via command prompt. In order to execute the batch file from command prompt, we must set the path to the directory where the batch file is stored or we should include the path address to that directory.
WHY use batch file programming?!
Here are few examples that you might find batch programming useful:
1. Open Multiple Programs Using a Batch File
If you have a number of programs you open each time you turn on your computer, you can use a batch file to automate the process!
Instead of opening each program manually, you can open them automatically all at once.
In my example, I will be opening two programs (chrome web-browser and notepad):
Here is explanation of each line of code:
@echo off as mentioned before, hides the commands.
start does the obvious and starts the program.
exit command will close your script.
2. Countdown program:
title Enter time to countdown from.
echo Enter time to countdown from.
set /a time=%time%-1
if %time%==0 goto timesup
title Counting down...
echo Countdown - [%time%]
ping localhost -n 2 > nul
echo Time is Up!!
Now try it yourself, copy and paste above code to notepad and see how above program works!
Here are more examples of cool batch applications!