title: How to substitute variable contents in a Windows batch file
body: <p>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</p>
  
  <pre><code>SET a=The fat cat
  ECHO %a%
  REM Results in 'The fat cat'
  ECHO %a:fat=thin%
  REM Results in 'The thin cat'
  </code></pre>
  
  <p>Works fine (output is 'The fat cat' and 'The thin cat'</p>
  
  <p>However, if 'fat' or 'thin' are in variables, it doesn't work</p>
  
  <pre><code>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!'
  </code></pre>
  
  <p>I know this can be done as I've seen it in blogs before, but I never saved the link to the blogs.</p>
  
  <p>Anyone have any ideas?</p>
  
  <p>PS. I'm running the scripts on Windows 7</p>
  
  <p>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.</p>
  
  <p>PPPS. I've also tried the scripts with delayed expansion explicitly turned on</p>
  
  <pre><code>SETLOCAL enabledelayedexpansion
  </code></pre>
  
  <p>This makes no difference.</p>