CMD variable substitution for parameters

cmd, shell, windows

Solution

The following (batch file) should work and prints "foo.baz":

  setlocal enabledelayedexpansion
  set FOO=foo.bar
  set BAR=bar
  set BAZ=baz

  echo !FOO:%BAR%:%BAZ%!

(About the FOR-loop thing you mention, you need to give more information on what exactly you mean.)

Problem

I'm trying to get the CMD equivalent of the following `bash` feature: ``` $ FOO=foo.bar $ BAR=bar $ BAZ=baz $ echo ${FOO/$BAR/$BAZ} foo.baz ``` Now, CMD has somehwat similar command subsitution when both the pattern and the substitution are constant: ``` C:\>set FOO=foo.bar C:\>set BAR=bar C:\>set BAZ=baz C:\>echo %FOO:bar=baz% foo.baz ``` However, I can't seem to reference variables in there - ``` C:\>echo %FOO:%BAR%=%BAZ%% %foo:bar=baz% ``` How would I do that? Bonus points for pointing out something that also works inside a `FOR` loop in a batch file.

Original source