Batchography: How to do string substitution in the Batch scripting language?

There are so many undocumented or obscure features in the Batch scripting language and in this article I am going to illustrate how to do string substitution.

For more advanced Batch scripting topics, please grab a copy of the Batchography book.

batchography-good-res

Let’s get started!

flower separator

Use the following syntax to achieve substitution operations on environment variables before they are evaluated:

%VarName:StringToBeMatched=StringToBeReplacedWith%

When delayed environment variable expansion (DEVE) is enabled, use the following syntax when appropriate:

!VarName:StringToBeMatched=StringToBeReplacedWith!

So basically, in Batch scripts, to expand an environment variable, just wrap the variable name with “%” (or “!”) from both sides.

To achieve string substitution, instead of just wrapping the variable from both sides, you put the column (“:”) character right after the environment variable name to specify that you want to start the substitution, then type the string value to be matched followed by the equal sign and the string to be replaced with.
Let me illustrate the syntax using a simple example (str-substitute.bat):

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

SET str1=Writing code using the Python scripting language. (1)
ECHO Regular expansion: %str1:Python=Batch% (2)

ECHO With DEVE: !str1:Python=Batch! (3)

ENDLOCAL

Which produces the following output when executed:

Regular expansion: Writing code using the Batch scripting language.
With DEVE: Writing code using the Batch scripting language.

At marker (1), we create an environment variable called str1 containing the string “Python” with the intent of replacing it with the word “Batch” (at markers (2) and (3)).

The string substitution syntax also allows you to start your search string with an asterisk (“*”). If that was the case, then everything from the beginning of the string up to your search string will be matched. Let me illustrate this with an example:

@echo off

setlocal enabledelayedexpansion

for %%a in (
 "01:31:2016 at 4:17AM - LOGENTRY: User 'Jane' has logged in"
 "01:31:2016 at 4:17AM - LOGENTRY: File 'payroll.db' has been accessed"
 "01:31:2016 at 4:55AM - LOGENTRY: User 'Jane' logged out"
 ) do (
 set V=%%~a (1)
 set V=!V:*LOGENTRY: =! (2)
 echo !V!
 )
 endlocal

Each test string has the following format: date/time stamp, followed by a hyphen, then the “LOGENTRY:” string and finally the actual log text. This structured line format makes it easy for us to extract just the log the text using the wildcard match-and-replace syntax as illustrated at marker (2).  Remember that string-substitution is only available for environment variables. Therefore, we have to transfer values to an environment variable first (code at marker (1)) then do the string substitution. At marker (2), we match “*” (meaning anything) up to the “LOGENTRY: “ text and then replace all that was matched with nothing; the DEVE syntax has been used because we are in a compound statement.
Let’s run the str-substitute-asterisk.bat script and observe the output:

C:\BatchProgramming>str-substitute-asterisk.bat
User 'Jane' has logged in
File 'payroll.db' has been accessed
User 'Jane' logged out

C:\BatchProgramming>

The output is as expected and we managed to replace all the text prior to our match string (“LOGENTRY:”) with an empty string (thus removing that text).
flower separatorYou might also like:

Leave a Reply