Introduction to Batch Scripting

What is Batch Scripting?

  • Batch scripting is a way to automate repetitive tasks using a series of commands stored in a text file.
  • These files, typically with a .bat extension, are executed by the command prompt (cmd.exe) in Windows.

Benefits:

  • Saves time and effort by automating tasks.
  • Reduces errors by ensuring commands are executed consistently.
  • Can improve organization and clarity for complex tasks.

Basic Structure:

  • Batch scripts are plain text files containing lines of commands.
  • Each line is executed sequentially, one after the other.
  • Lines starting with REM or :: are comments and ignored by the interpreter.

Common Commands:

  • @echo off: Hides the commands being executed and the “echo off” message itself (commonly used at the beginning of scripts).
  • cd: Changes the current directory.
  • dir: Lists files and folders in the current directory.
  • copy: Copies files.
  • ren: Renames files.
  • del: Deletes files.
  • if: Executes commands conditionally based on a specific condition.
  • for: Executes a set of commands repeatedly for a list of items.

Types of Variables​

There are two types of variables in batch files. One is for parameters that can be passed when the batch file is called, and the other is done through the set command.

Command Line Arguments​

Batch scripts support the concept of command-line arguments, in which arguments can be passed to the batch file when it is invoked.

The arguments can be called from the batch files through the variables %1, %2, %3, and so on.

For example, the batch script below accepts 3 command line arguments and echo’s them to the command line screen:

parameter-test.bat

@echo off
echo %1
echo %2
echo %3

If the above batch script is stored in a file called parameter-test.bat and we were to run the batch as:

C:\my\path>parameter-test.bat 1 2 3

And the above command produces the following output:

1
2
3

set command​

The other way to initialize variables is the set command. The syntax of the set command is as follows.

set /A variable_name=value

where:

  • variable_name is the name of the variable you want to set.
  • value is the value which needs to be set against the variable.
  • /A is used if the value needs to be numeric.

For example, in this script a variable called message is defined and set with the value of “Hello World”.

set-example.bat

@echo off
set message=Hello World
echo %message%


And the above command produces the following output:

Hello World 

Numeric Variables​

It is possible to define a variable to hold a numeric value. This can be done by using the /A switch (also known as flag).

For example:

numeric-example.bat

@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%

where:

  • we assign the values 5 and 10 to two variables a and b respectively.
  • we sum these values and store the result in the variable c.
  • Finally, we display the value of the variable c.

The output of the above script is 15.

Arithmetic Operators​

All the arithmetic operators work in batch files.

For example:

arithmetic-example.bat

@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%
SET /A c = %a% - %b%
echo %c%
SET /A c = %b% / %a%
echo %c%
SET /A c = %b% * %a%
echo %c%

And the output is:

15
-5
2
50

Batch Script Comments​

Comments in Batch Script can be made in two ways:

  • using the Rem statement
  • using the :: statement

Comments Using the Rem Statement​

Comments in Batch Script can be made using the Rem command.

Any text which follows the Rem statement will be treated as comments and will not be executed.

Rem Example

Rem My comment

Comments Using the :: Statement​

The other way to create comments in Batch Script is via the :: command.

Any text which follows the :: statement will be treated as comments and will not be executed.

:: Example

:: My comment

where My comment is the comment that needs to be added.

Like the Rem command, the :: command is used to describe what the program does.

HelloWorld.bat

@echo off 
:: This program displays Hello World 
set message=Hello World 
echo %message%
Hello World

Questions? : Reach Out