Batch Script String Manipulation

When working with a string of characters, Batch Script has built in functionality that allows for them to be easily manipulated.

When working with the console, a string can be displayed using the ‘echo’ command.

echo Hello World!

If it is necessary to incorporate the value of a variable into this message it can be done by including a percentage sign before and after the variable name.

set fname=Fred
set sname=Bloggs

echo Hello %fname% %sname%!

This will display the message, 'Hello Fred Bloggs!' in the console.

This syntax can also be used to populate a variable.

set fname=Fred
set sname=Bloggs

set message=Hello %fname% %sname%!

echo %message%

The same message is displayed as before in the console.

Index Value and Substring Functionality

Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on. This index value can be used to return a small portion of a larger string.

The substring functionality is incorporated after the variable name, but before the ending percentage sign. Below are examples of how to extract a portion of a string from the start, middle and end.

set fname=Fred
set sname=Bloggs

set message=Hello %fname% %sname%!

echo %message:~0,5%
echo %message:~6,4%
echo %message:~-7%

The substring functionality starts with ':~'. What follows depends on what portion of the larger string is required. To return a portion of the string from the start, the tilde is followed by a zero, then a comma, followed by the number of characters required, as shown in the first example above.

To return the middle portion of the string, the index position of the first desired character is included after the tilde, followed by a comma, and again, the number of characters required. This is shown in the second example above.

Finally, to extract a number of characters from the end of the string, a minus value is included after the tilde, for example minus seven, to return the last seven characters, as shown in the last example above.

These examples will produce the following output.

Hello
Fred
Bloggs!

Replace and Remove Functionality

If desired, it is possible to replace part of a string with another value.

set fname=Fred
set sname=Bloggs

set message=Hello %fname% %sname%!

echo %message%
echo %message:Fred=Joe%

Here, the word 'Fred' in the message 'Hello Fred Bloggs!' is replaced with 'Joe'.

Hello Fred Bloggs!
Hello Joe Bloggs!

Note that if the word 'Fred' appeared multiple times in the message, then all occurrences would be replaced with 'Joe' and not just the first one.

This same functionality can be used to remove part of a string.

set fname=Fred
set sname=Bloggs

set message=Hello %fname% %sname%!

echo %message%
echo %message: Bloggs=%

With the above example, a space, followed by the word 'Bloggs' is removed from the message.

Hello Fred Bloggs!
Hello Fred!

If for some reason all spaces need to be removed, this can be achieved in a similar fashion.

set fname=Fred
set sname=Bloggs

set message=Hello %fname% %sname%!

echo %message%
echo %message: =%

The resulting output is as follows.

Hello Fred Bloggs!
HelloFredBloggs!