Batch Script String Manipulation
Batch Script String Manipulation
In Batch Script, a string is an ordered collection of characters.
Batch scripts have the following commands which are used to carry out string manipulation in strings:
%variable:~num_chars_to_skip%
%variable:~num_chars_to_skip,num_chars_to_keep%
and this can include negative numbers:
%variable:~num_chars_to_skip, -num_chars_to_keep%
%variable:~-num_chars_to_skip,num_chars_to_keep%
%variable:~-num_chars_to_skip,-num_chars_to_keep%
The above commands will be useful for some manipulations explained below (e.g., for substrings).
The most common operations on strings in Batch Scripting are explained below:
Create a String
A string can be created using the set
command in the following way:
@echo off
:: This program displays Hello World
set message=Hello World
echo %message%
Hello World
Empty String
An empty string can be created by assigning no value to it during initialization:
set variable_name=
To check for an empty string, you need to enclose the variable name in square brackets and compare it with a value in square brackets:
if [%variable_name%]==[] echo "True"
Below is a complete example with empty strings:
@echo off
set a=
set b=Hello
if [%a%]==[] echo "String A is empty"
if [%b%]==[] echo "String B is empty "
String A is empty
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.
In Batch Scripting, the string interpolation can be done using the set command and lining up the numeric defined variables or any other literals in one line when using the set command.
@echo off
set a=Hello
set b=World
set /A d=123
set c=%a% and %b% %d%
echo %c%
Hello and World 123
String Concatenation
You can use the set operator to concatenate two strings or a string and a character, or two characters
@echo off
SET a=Hello
SET b=World
SET c=The %a% and the %b%
echo %c%
The Hello and the World
String Length
In Batch Scripting there is no length function defined to find the length of a string.
But there are custom-defined functions which can be used for the same.
For example, you can reuse what is defined below:
@echo off
set str=Hello World
call :strLen str strlen
echo String is %strlen% characters long
exit /b
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
goto :eof
11
Some points about the example above:
- The actual code which finds the length of string is defined in the
:strLen
block. - The length of the string is maintained in the variable
len
.
String to Integer
A variable that has been set as a string using the set variable can be converted to an integer using the /A
flag using the set
variable.
@echo off
set var=12345
set /A var=%var% + 5
echo %var%
12350
Align Right
It is used to align text to the right, normally to improve readability of columns of numbers.
In the following example we add leading spaces to a string to make sure the output lines up: we want for variables no longer than 8 characters to add 8 spaces at the beginning and then show only the last 8 characters of the variable.
@echo off
set x=3000
set y=2
set x=%x%
set y=%y%
echo.X=%x:~-8%
echo.Y=%y:~-8%
X= 3000
Y= 2
Note that the value 2
is aligned with the unit columns when displaying the numbers.
The echo.
command is used to write a blank line on the screen.
Substring starting at the beginning of the string (Left string)
This is used to extract characters from the beginning of a string.
The substring syntax is as follows:
set substring=%variable_name:~from_index,to_index%
To extract characters from the beginning of a string you need to use zero as first index, i.e. %variable_name:~0,to_index%
An example:
set str=Helloworld
echo.%str%
set str=%str:~0,5%
echo.%str%
Helloworld
Hello
Substring (Mid string)
This is used to extract a substring via the position of the characters in the string.
The substring syntax is as follows:
set substring=%variable_name:~from_index,to_index%
@echo off
set str=Helloworld
echo %str%
set str=%str:~3,7%
echo %str%
Helloworld
loworld
Substring starting at the end of the string (Right string)
This is used to extract characters from the end of a string.
The substring syntax is as follows:
set substring=%variable_name:~-index%
To extract characters from the end of a string, it is necessary to use -
followed by the number of characters to be extracted, i.e. %variable_name:~-index%
.
@echo off
set str = This message needs changed.
echo %str%
set str = %str:~-8%
echo %str%
This message needs changed.
changed.
Remove
The string substitution feature can also be used to remove a substring from another string.
The syntax is as follows:
set newstr=%variable_name:stringtobereplaced=newstring%
To remove a substring, you must leave the newstring
parameter in %variable_name:stringtobereplaced=newstring%
empty.
An example:
@echo off
set str=Batch scripts is easy. It is really easy.
echo %str%
set str=%str:is =%
echo %str%
Batch scripts is easy. It is really easy.
Batch scripts easy. It really easy.
Remove Both Ends
This is used to remove the first and the last character of a string.
The syntax is as follows:
set substring=%variable_name:~from_index,to_index%
To remove both ends from a string, it is necessary to substring from the second character to the second-to-last, i.e. %variable_name:~1,-1%
.
An example:
@echo off
set str=Batch scripts is easy. It is really easy
echo %str%
set str=%str:~1,-1%
echo %str%
Batch scripts is easy. It is really easy
atch scripts is easy. It is really eas
Remove All Spaces
This is used to remove all spaces in a string by substitution.
The syntax is as follows:
set substring=%variable_name: =%
An example:
@echo off
set str=This string has a lot of spaces
echo %str%
set str=%str: =%
echo %str%
This string has a lot of spaces
Thisstringhasalotofspaces
Replace a String
To replace a substring with another string use the string substitution feature.
The syntax is as follows:
set newstr=%variable_name:stringtobereplaced=newstring%
To replace a substring, you must define the values of the stringtobereplaced
and newstring
parameters in %variable_name:stringtobereplaced=newstring%
.
@echo off
set str=This message needs changed.
echo %str%
set str=%str:needs=has%
echo %str%
This message needs changed.
This message has changed.