[927] Batch script related
ref: Batch Script - Quick Guide
ECHO Command
@echo off
By default, a batch file will display its command as it runs. The purpose of this first command is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well.
Documentation
Very often batch files also contains lines that start with the "Rem" command. This is a way to enter comments and documentation. The computer ignores anything on a line following Rem. For batch files with increasing amount of complexity, this is often a good idea to have comments.
Command Line Arguments
Batch scripts support the concept of command line arguments wherein arguments can be passed to the batch file when invoked. The arguments can be called from the batch files through the variables %1, %2, %3, and so on.
Set Command
The other way in which variables can be initialized is via the ‘set’ command. Following is the syntax of the set command.
Syntax
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 – This switch is used if the value needs to be numeric in nature.
Local vs Global Variables
In any programming language, there is an option to mark variables as having some sort of scope, i.e. the section of code on which they can be accessed. Normally, variable having a global scope can be accessed anywhere from a program whereas local scoped variables have a defined boundary in which they can be accessed.
DOS scripting also has a definition for locally and globally scoped variables. By default, variables are global to your entire command prompt session. Call the SETLOCAL command to make variables local to the scope of your script. After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL, calling EXIT, or when execution reaches the end of file (EOF) in your script.
Working with Environment Variables
If you have variables that would be used across batch files, then it is always preferable to use environment variables. Once the environment variable is defined, it can be accessed via the % sign.
Comments Using the Rem Statement
There are two ways to create comments in Batch Script; one is via the Rem command. Any text which follows the Rem statement will be treated as comments and will not be executed. Following is the general syntax of this statement.
Syntax
Rem Remarks
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. Following is the general syntax of this statement.
Syntax
:: Remarks
Note − If you have too many lines of Rem, it could slow down the code, because in the end each line of code in the batch file still needs to be executed.
In DOS, a string is an ordered collection of characters, such as "Hello, World!".
S.No | Strings & Description |
---|---|
1 | Create String
A string can be created in DOS in the following way. |
2 | Empty String
Empty String |
3 | String Interpolation
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. |
4 | String Concatenation
You can use the set operator to concatenate two strings or a string and a character, or two characters. Following is a simple example which shows how to use string concatenation. |
5 | String length
In DOS scripting, there is no length function defined for finding the length of a string. There are custom-defined functions which can be used for the same. Following is an example of a custom-defined function for seeing the length of a string. |
6 | toInt
A variable which has been set as string using the set variable can be converted to an integer using the /A switch which is using the set variable. The following example shows how this can be accomplished. |
7 | Align Right
This used to align text to the right, which is normally used to improve readability of number columns. |
8 | Left String
This is used to extract characters from the beginning of a string. |
9 | Mid String
This is used to extract a substring via the position of the characters in the string. |
10 | Remove
The string substitution feature can also be used to remove a substring from another string. |
11 | Remove Both Ends
This is used to remove the first and the last character of a string. |
12 | Remove All Spaces
This is used to remove all spaces in a string via substitution. |
13 | Replace a String
To replace a substring with another string use the string substitution feature. |
14 | Right String
This is used to extract characters from the end of a string. |
ref: Batch Script - Decision Making
Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
S.No | Strings & Description |
---|---|
1 | If Statement
The first decision-making statement is the ‘if’ statement. |
2 | If/else Statement
The next decision making statement is the If/else statement. Following is the general form of this statement. |
3 | Nested If Statements
Sometimes, there is a requirement to have multiple ‘if’ statement embedded inside each other. Following is the general form of this statement. |
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
In batch script, the following types of operators are possible.
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Bitwise operators
Arithmetic Operators
Batch script language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available.
Operator | Description | Example |
---|---|---|
+ | Addition of two operands | 1 + 2 will give 3 |
− | Subtracts second operand from the first | 2 − 1 will give 1 |
* | Multiplication of both operands | 2 * 2 will give 4 |
/ | Division of the numerator by the denominator | 3 / 2 will give 1.5 |
% | Modulus operator and remainder of after an integer/float division | 3 % 2 will give 1 |
Relational Operators
Relational operators allow of the comparison of objects. Below are the relational operators available.
Operator | Description | Example |
---|---|---|
EQU | Tests the equality between two objects | 2 EQU 2 will give true |
NEQ | Tests the difference between two objects | 3 NEQ 2 will give true |
LSS | Checks to see if the left object is less than the right operand | 2 LSS 3 will give true |
LEQ | Checks to see if the left object is less than or equal to the right operand | 2 LEQ 3 will give true |
GTR | Checks to see if the left object is greater than the right operand | 3 GTR 2 will give true |
GEQ | Checks to see if the left object is greater than or equal to the right operand | 3 GEQ 2 will give true |
Logical Operators
Logical operators are used to evaluate Boolean expressions. Following are the logical operators available.
The batch language is equipped with a full set of Boolean logic operators like AND, OR, XOR, but only for binary numbers. Neither are there any values for TRUE or FALSE. The only logical operator available for conditions is the NOT operator.
Operator | Description |
---|---|
AND | This is the logical “and” operator |
OR | This is the logical “or” operator |
NOT | This is the logical “not” operator |
Assignment Operators
Batch Script language also provides assignment operators. Following are the assignment operators available.
Operator | Description | Example |
---|---|---|
+= | This adds right operand to the left operand and assigns the result to left operand |
Set /A a = 5 a += 3 Output will be 8 |
-= | This subtracts the right operand from the left operand and assigns the result to the left operand |
Set /A a = 5 a -= 3 Output will be 2 |
*= | This multiplies the right operand with the left operand and assigns the result to the left operand |
Set /A a = 5 a *= 3 Output will be 15 |
/= | This divides the left operand with the right operand and assigns the result to the left operand |
Set /A a = 6 a/ = 3 Output will be 2 |
%= | This takes modulus using two operands and assigns the result to the left operand |
Set /A a = 5 a% = 3 Output will be 2 |
Bitwise Operators
Bitwise operators are also possible in batch script. Following are the operators available.
Operator | Description |
---|---|
& | This is the bitwise “and” operator |
| | This is the bitwise “or” operator |
^ | This is the bitwise “xor” or Exclusive or operator |
Following is the truth table showcasing these operators.
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
ref: Batch Script - DATE and TIME
ref: Batch Script - Input / Output
Redirecting Output (Stdout and Stderr)
One common practice in batch files is sending the output of a program to a log file. The > operator sends, or redirects, stdout or stderr to another file. The following example shows how this can be done.
Dir C:\ > list.txt
Suppressing Program Output
The pseudo file NUL is used to discard any output from a program. The following example shows that the output of the command DIR is discarded by sending the output to NUL.
Dir C:\ > NUL
Stdin
To work with the Stdin, you have to use a workaround to achieve this. This can be done by redirecting the command prompt’s own stdin, called CON.
The following example shows how you can redirect the output to a file called lists.txt. After you execute the below command, the command prompt will take all the input entered by user till it gets an EOF character. Later, it sends all the input to the file lists.txt.
TYPE CON > lists.txt
ref: Batch Script - Return Code
Loops
In the decision making chapter, we have seen statements which have been executed one after the other in a sequential manner. Additionally, implementations can also be done in Batch Script to alter the flow of control in a program’s logic. They are then classified into flow of control statements.
S.No | Loops & Description |
---|---|
1 | While Statement Implementation
There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels. |
2 | For Statement - List Implementations
The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values. |
3 | Looping through Ranges
The ‘for’ statement also has the ability to move through a range of values. Following is the general form of the statement. |
4 | Classic for Loop Implementation
Following is the classic ‘for’ statement which is available in most programming languages. |
Looping through Command Line Arguments
The ‘for’ statement can also be used for checking command line arguments. The following example shows how the ‘for’ statement can be used to loop through the command line arguments.
-
Uses the echo off command to ensure that the commands are not shown when the code is executed.
-
The Rem command is used to add a comment to say what exactly this batch file does.
-
The dir command is used to take the contents of the location C:\Program Files.
-
The ‘>’ command is used to redirect the output to the file C:\lists.txt.