Introduction to Batch File Programming

This is first tutorial on Batch File Programming. So, I thought of starting with the basics and moving on the more advanced tutorials.

What is a Batch File?

A batch file, which is also called batch program or script is a set of commands that simplify our repetitive and tedious tasks. Each line in a batch file is a command. These commands are logically grouped to form a script that is interpreted to carry out the intended task. A batch file is a simple text file saved with an extension “.bat”. These batch commands are case insensitive and easier to write and learn.

Introduction to Batch File Programming
Image Source

 

How to Create a Batch File?

You don’t need a specialized IDE (Integrated Development Environment) to create these batch scripts but a simple text editor is more than enough.  Also it becomes boring to write, save and then run batch files if you write a lot of them. Debugging of these files becomes harder if you write very complex programs without an IDE. So, I suggest you to download and start using a simple IDE developed at code project. It’s very simple to use, you get used within few days with it (Don’t expect me to explain how to use of it). Follow below link to download it.

http://www.codeproject.com/Tips/888466/Batch-IDE

How to Execute a Batch File?

Batch file can be thought of as a Windows version of Unix Shell. Though Batch scripts are not as flexible as shell scripts, they appear to be similar in function. The shell program COMMAND.COM or cmd.exe reads the plain text file (with extension “.bat”) and executes its commands.

You can include any number of commands in a single batch file and there is no limit for it. Basically when we run a batch file in the command prompt the command line interface executes the commands in the batch file in a sequential order normally line by line. This means that the CLI (Command Line Interface) acts as an interpreter to the batch scripts. Batch files support various arithmetic, boolean, logical operations just like all other programming languages.

You may include any commands in a batch file. Even there are certain conditional commands like ‘goto’, ‘if’ that carries out commands based on the results of the condition, looping statements such as ‘for’ to iterate over a specific set of commands for a desired value of iterator, commands that allow you to control input, output and call to other batch files.

Modes of operation of a Batch File

There are two different modes that a batch file can operate, classified as

1. Interactive Mode.
2. Batch Mode or Silent Mode

Interactive Mode

In this mode of operation, based on the user input, the script decides the further commands that are to be executed. The program waits till it receives an input from the user to proceed. For example, consider the ‘del’ command used for deleting files that reside inside ‘crazyprogrammer’ directory. Now I am writing a command to delete all the files inside a folder named ‘crazyprogrammer’.

C:>del crazyprogrammer
C:crazyprogrammer*, Are you sure (Y/N)? Y

When I executed the above command, it is interacting with me prompting “Are you sure (Y/N)?” confirming the deletion operation, and depending on the input given, it decides what to do. If I hit ‘Y’ then it will delete the directory, else if I hit ‘N’ then the script stops.

Batch Mode

Batch mode is Quiet Mode of operation of the script. The commands that operate in batch mode will not interact with the user, instead it will take care of every operation by itself. For example, consider same ‘del’ command. There is a switch ‘/Q’ available for the ‘del’ command, which makes it to operate at silent mode.

C:>del /Q crazyprogrammer
C:>

In this case the command did not interact with me and deleted the directory directly. This mode is used to automate the task when the user interference is not required.

Classification of Commands

Commands that can be contained in a batch script are classified as two types

1. Internal commands
2. External commands

Internal Commands

Internal commands are the built-in commands that are designed along with the operating system, like echo, cls, del, dir were few of the useful internal commands.

External Commands

External commands are created when a new application is installed and these commands mostly application specific calls such as javac to compile java files, firefox command to start Firefox.

If your machine doesn’t have Firefox browser installed then these commands are of no use. So, they are named as external commands.

1 thought on “Introduction to Batch File Programming”

  1. here i can make a batch file about folder replication using loop in notepad
    open notepad & type the following

    cd
    cd C:UsersDocuments & Settingsusernamedesktop
    :loop
    md malware
    cd malware
    goto loop

    save it as .bat or .exe

Leave a Comment

Your email address will not be published. Required fields are marked *