Batch Script Introduction
What is a Batch Script?
A Batch Script is text file containing lines with commands that get executed in sequence by the Microsoft command interpreter (cmd.exe).
Batch scripts are stored in text files containing lines with commands that get executed in sequence, one after the other.
Batch scripts files have the special extension BAT (.bat
) or CMD (.cmd
). This type of file is recognized by the Operating System and executed through an interface (called shell) provided by a system file called the command interpreter.
Running a batch file is a simple matter of just clicking on it.
Batch files can also be run in a command prompt or the Start-Run line. In this case, the full path must be used, unless the file is in the path environment.
Features and Uses of Batch Script
Some of the features of Batch Script:
- It can read user input so that it can process it further.
- It has control structures such as
for
,if
,while
,switch
for better automation and scripting. - It supports advanced features such as
Functions
andArrays
. - It supports regular expressions.
- It can include other programming codes.
Some of the common uses of Batch Script are:
- Setting up servers for different purposes.
- Automating housekeeping activities such as deleting unwanted files or log files.
- Automating the deployment of applications from one environment to another.
- Installing programs on various machines at once.
What is the Windows command interpreter?
The command interpreter is a file responsible for handling and processing the command done at the MS-DOS or Windows command line interface.
The command interpreter for Windows is cmd.exe.
This application is responsible for creating the command window, accepting configuration commands, and accessing built-in commands such as the Dir command.
For Windows scripting, cmd.exe is a legacy technology; a modern equivalent is PowerShell, which is based on .NET.
PowerShell's capabilities vastly outstrip those of cmd.exe
Interpretation of a Command line
Parsing a command line into a sequence of commands is complex.
There are four main things to consider:
- Variable substitution: A command line is scanned for variable specifications, and any found are replaced with the contents of those variables.
- Quoting: Special characters can be quoted, to remove their special meanings.
- Syntax: Command lines are developed into a sequence of commands according to a syntax.
- Redirection: Redirection specifications are applied, and removed from the command line, before an individual command in a sequence is executed.