|
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.
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 :-)
![]() ![]() |
| ![]() ![]() |