Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: SleepSeconds() - delay processing for the specified number of seconds[Next]: StringContains() - look for some text within another text string
\->Batch Files->String Replacement - case sensitive substitution of all or part of a string

String Replacement [case sensitive substitution of all or part of a string].cmd.txt

This allows you to replace some characters with others as demonstrated by the case conversion example.

The replacement retrieves environment variables with a slightly different syntax. Without textual replacement you'd say something like "%VariableName%", if you wanted to replace "before" with the text "after" you'd say this instead: "%VariableName:before=after%".

The main limitation around string replacement is that there is no case sensitive way of doing it (it substitutes any case strings). Another issue is that you can't replace anything that has a leading "*" as it has special meaning, a from string of "*UpTo" will replace everything from the start of the string up until the end of the "UpTo". See the ReplaceAsterisks() example for a workaround.

The source Environment Variable Must Not Be Empty!

The main Windows bug around substitution of substrings is that is that the string you are looking to change must not be empty! This is a major problem in batch files and complicates a simple replacement into 3 lines of code or 2 if you avoid the substitution with conditional code.

The last line in this batch file will not display the correct value:

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set STRING=This are a string of characters
set  EMPTY=

::--- The second line will fail the string replacement ---
echo After Subtitution, STRING = "%STRING:are=is%"
echo After Subtitution, EMPTY  = "%EMPTY:are=is%"

If "ENABLEEXTENSIONS" is on you can do something like: something like:

...
::echo After Subtitution, EMPTY  = "%EMPTY:are=is%"
set Fixed=
::[Will fail if string contains double quotes]: if not "%EMPTY%" == "" set Fixed=%EMPTY:are=is%
IF DEFINED EMPTY set Fixed=%EMPTY:are=is%
echo After Subtitution, EMPTY  = "%FIXED%"

The other tip is to ensure the value is not empty by adding a character to the start and removing it after the substitution with substring operations:

...
::echo After Subtitution, EMPTY  = "%EMPTY:are=is%"
set Fixed=%EMPTY%
call :ReplaceString "EMPTY" "are" "is"

echo After Subtitution, EMPTY  = "%FIXED%"
goto :EOF

::+++++++++++++++++++
:ReplaceString
::  P1 = NAME OF ENV VAR (MS BUG: need to handle value of "")
::  P2 = Before string
::  P3 = After string
::  NEEDS: ENABLEDELAYEDEXPANSION
::+++++++++++++++++++
    set VAL=#!%~1!
    set VAL=!VAL:%~2=%~3!
    set %~1=%VAL:~1%
    goto :EOF

Conditional Logic Based on a String's CONTENTS!

Testing if a string is equal to another (case sensitive or not) is easy enough with the "IF" command but what if you want to see if it contains something?

Using string replacement (as mentioned above its only case insensitive) you can try to make the string you are searching for different and then look for the change.

@echo off
setlocal ENABLEEXTENSIONS

set STRING=This are a strinG of characters

set DifferentIfContains=#%STRING:String=###MAKE-DIFFERNT###%
echo The text "%STRING%":
if     "#%STRING%" == "%DifferentIfContains%" echo   * Doesn't contain the text "String"
if not "#%STRING%" == "%DifferentIfContains%" echo   * Certainly does contain the text "String" :-)

See the StringContains() example for a easy to use subroutine you could use.

[anchor]

The Code for: "String Replacement [case sensitive substitution of all or part of a string].cmd.txt"

This is the example, a shortcut was installed to this code :

$todo

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: SleepSeconds() - delay processing for the specified number of seconds[Next]: StringContains() - look for some text within another text string


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.