Many of us have heard or perhaps known about the batch file, but very few are aware of its power and dominance in Windows. Almost everything can be done when we know relevant command line instructions. So in this series of tutorials, we will learn about batch file programming and how we can execute command line instructions with a single click through them.
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?
click here to know in details about all the batch file commands with examples.
Well as simple as it sounds, you don’t need any extra software installed to create a batch file.
Just open up a built-in text editor for windows.
After writing commands, all you need to do is save it as a .bat
or .cmd
file.
Voila, you have created your first ever batch file. But you haven’t put any instructions. We will cover about programming and scripting in next articles.
Now that you know how to create a batch file, you must be wondering how to run it?
[adsense1]
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.
Let’s create a simple batch script to display “This is my first script”.
@echo off
echo This is my first script
pause
Line 1:
@echo off
If we don’t put @echo off
at the top of the script, it will produce an output where ‘echo’ itself will also be displayed. And the output becomes like:
So to avoid the display of command itself, we must use @echo off at the top.
Line 2:
echo This is my first script
Line 2 just echoes ‘This is my first script’ onto the console.
Line 3: Pause
is used to hold the screen until we press a key. If pause
is not used, the output screen will vanish away within a blink of an eye and we won’t be able to see the output.
You must always follow best programming practice while writing codes, be it batch file programming or any.
Even in short programs, we must maintain the habit of following better practice because while we write huge programs, it becomes a nightmare to debug it and also maintain it because no one else will understand your code if not properly documented.
So here are the few things that must be implemented and few things that must be avoided while coding.
DOs
REM
or : :
is used for comments in batch file programming. Here is the example.
REM This is first comment :: This is another comment
DON’Ts
If there are few things to be considered for better programming practice, there are also certain things that must be avoided. Here are the few things that you as a programmer should try to avoid while coding.
Now that you have known about the batch files, in the coming tutorials, you will learn about advanced concepts in batch file programming.
But we should warn you that, batch file commands and scripts are too much powerful and if used without proper knowledge, it can crash your machine and software’s functionalities. Make sure you know what you are doing because with batch scripts we are playing with the core feature of Windows.