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.
Batch File Programming tutorial
Batch File Programming – Introduction
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.
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?
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
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?
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.
Let’s create a simple batch script to display “This is my first script”.
- First, open up a text editor and save the file as a .bat file
@echo off echo This is my first script pause
- Copy and paste the code above and click on that file to run the script
It will generate an output like this.
Explanation of the program
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.
echo This is my first script
Line 2 just echoes ‘This is my first script’ onto the console.
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.
Batch File Programming – DOs and DON’Ts
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.
- Documenting code with comments
Perhaps this is one of the most important one because without proper documentation it becomes tedious to maintain the code and debug it .
So it is always a good idea to insert comments in programs or codes, explaining what the next lines or block of code is trying to accomplish, how and why.
: :is used for comments in batch file programming. Here is the example.
REM This is first comment :: This is another comment
- Validating input
As simple as it is to code batch files, so is to hack and tweak the code because everything is like plain English in batch programs. Batch files are weakly typed, so it is always a better approach to validate all the inputs in batch file programming.
- Check variables before using them
Always check the new variable before using them or initializing them because they might already have been defined.
Without proper indentation, a program becomes confusing to intercept. So always use proper indentation in every line of code for better understanding of the code.
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.
- Avoid one-liner codes (multiple command lines joined into a single one by ANDs and ORs) and use a block of code.
- Avoid nested block codes (nested if else) and use subroutines instead.
- Don’t use variable names as command names
Some final words of advice before you start playing around batch commands
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.