I’m writing a simple script to substitute text in an environment variable with other text. The trouble I get is with the substituted or substituted text being pulled from other variables
SET a=The fat cat
ECHO %a%
REM Results in 'The fat cat'
ECHO %a:fat=thin%
REM Results in 'The thin cat'
Works fine (output is ‘The fat cat’ and ‘The thin cat’
However, if ‘fat’ or ‘thin’ are in variables, it doesn’t work
SET b=fat
ECHO %a:%c%=thin%
REM _Should_ give 'The thin cat'.
REM _Actually_ gives '%a:fat=thin%' (the %c% is evaluated, but no further).
REM using delayed evaluation doesn't make any difference either
ECHO !a:%c%=thin!
REM Actual output is now '!a:fat=thin!'
I know this can be done as I’ve seen it in blogs before, but I never saved the link to the blogs.
Anyone have any ideas?
PS. I’m running the scripts on Windows 7
PPS. I know this is easier in Perl/Python/other script language of choice, but I just want to know why something that should be easy is not immediately obvious.
PPPS. I’ve also tried the scripts with delayed expansion explicitly turned on
SETLOCAL enabledelayedexpansion
This makes no difference.
Please try the following:
Copy and paste the code into Notepad and save it as a batch file.
I hope you’re convinced!