0

I see this is a common theme, but I believe I have a unique problem.

I want to use this technique of variable expansion, where you can use the ":" to substitute strings in expanded result (the "&" part being a trick to force command execution)

echo %PATH:;=&echo.%

, but like this:

set VAR=PATH
set DELIM=;
set SUBST=echo.

echo %VAR:%DELIM%=&%SUBST%%

Well, I hope you get the point. The above is not valid code.

Anyone has a clue?

The example in the question should be considered generic, and not to be taken literally. The question is can it be done and exactly how?

Ate Somebits
  • 287
  • 1
  • 18
  • Feel free to change the title to something more meaningful... – Ate Somebits Jun 12 '18 at 11:53
  • 2
    There are some ways: 1. `call echo %%VAR:%DELIM%=&%SUBST%%%`, or 2., when [delayed expansion](http://ss64.com/nt/delayedexpansion.html) is enabled, `echo !VAR:%DELIM%=&%SUBST%!`, or 3. `for /F "tokens=1* delims==" %%I in ("%DELIM%=%SUBST%") do echo !VAR:%%I=&%%J!`. I tend to call something like this "nested variables" by the way... And there is a typo, it is `SUBST` but not `SUBS`... – aschipfl Jun 12 '18 at 11:53
  • 1
    See [this post](https://stackoverflow.com/a/10167990) and also [this](https://stackoverflow.com/a/50401317) and [this](https://stackoverflow.com/a/44687985) and [this](https://stackoverflow.com/a/41122788)... – aschipfl Jun 12 '18 at 12:01
  • @aschipfl Thanks. All I get is `VAR:;=` in both 1., and 2. As for 3., that's a flow control, so it doesn't address the question. – Ate Somebits Jun 12 '18 at 12:13
  • @aschipfl +1 for the links, though. – Ate Somebits Jun 12 '18 at 12:17
  • 1
    Argh, sorry, I missed the `VAR`/`PATH` part: 1. `call echo %%%VAR%:%DELIM%=&%SUBST%%%`, or 2., when delayed expansion is enabled, `echo !%VAR%:%DELIM%=&%SUBST%!`, or 3. `for /F "tokens=1,2* delims=:=" %%H in ("%VAR%:%DELIM%=%SUBST%") do echo !%%H:%%I=&%%J!` (also with delayed expansion)... – aschipfl Jun 12 '18 at 12:28
  • No, my very blunt mistake. I forgot to change the variables in the actuall command, while changing them in the declarations. It was due to localized names which would not be understood. But, FYR, `call echo %%%VAR%:%DELIM%=&%SUBST%%%` yields `PATH:;=\n&` ("\n" is for the actual CRLF) – Ate Somebits Jun 12 '18 at 13:56
  • Before we go on, I will definitely read your posts in the links. It seems like you are well-versed in cmd language – Ate Somebits Jun 12 '18 at 14:00

0 Answers0