Substitution of CMD variables for parameters

I am trying to get the CMD equivalent of the following function bash:

$ FOO=foo.bar
$ BAR=bar
$ BAZ=baz
$ echo ${FOO/$BAR/$BAZ}
foo.baz

Now CMD has some havilny similar command substitution, when both the template 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 cannot reference the variables there -

C:\>echo %FOO:%BAR%=%BAZ%%
%foo:bar=baz%

How should I do it? Bonus points to indicate what also works inside the loop FORin a batch file.

+5
source share
2 answers

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

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

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

(About what you specify in the FOR-loop, you need to give more information about what exactly you mean.)

+4
source

From the command line:
call echo %FOO:%bar%=%baz%%

In batch and for loop:

@echo off
  set FOO=foo.bar
  set BAR=bar
  set BAZ=baz
for %%N in (baz ban bak) do (
 set BAZ=%%N_AndSomething
 call :expand 
)
goto :eof

:expand
call echo %%FOO:%BAR%=%BAZ%%%
goto :eof

, ( ), , . , for, .

+2

All Articles