Skip to main content

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%
note

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:

Example of String creation
@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:

Empty string 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:

Empty String Check
if [%variable_name%]==[] echo "True"

Below is a complete example with empty strings:

Example of Empty String
@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.

Example of String Interpolation
@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

Example of String Concatenation
@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:

Example of String Length
@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
note

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.

Example of String to Integer
@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.

Example of Align Right
@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

Note that the value 2 is aligned with the unit columns when displaying the numbers.

note

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%
note

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:

Example of Left String
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%
Example of Mid String
@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%
note

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%.

Example of Right String
@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%
note

To remove a substring, you must leave the newstring parameter in %variable_name:stringtobereplaced=newstring% empty.

An example:

Example of Remove
@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%
note

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:

Example of Remove Both Ends
@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:

Example of Remove All Spaces
@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%
note

To replace a substring, you must define the values of the stringtobereplaced and newstring parameters in %variable_name:stringtobereplaced=newstring%.

Example of Replace a String
@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.