0

I'd like to be able to expand the value of a REG_EXPAND_SZ retrieved with reg query <KEY> /v <VALUE>.

How do you expand an environment variable containing environment variables (surrounded with %)? For example, I want to expand %systemroot%\system32\config contained in x to C:\Windows\system32\config.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • Just use it. That's all you need do. –  Jul 26 '16 at 08:37
  • @Noodles That's not true. For example, if the environment variable `x` is `%UserProfile%\Documents`, where `echo '%x%'` results in `'%UserProfile%\Documents'`, you cannot use `dir "%x%"` or similar operations. The string contains a literaly percent symbol and normally would not be treated specially as an environment variable, even if it's desired. – Alyssa Haroldsen Jul 26 '16 at 09:17
  • You could also use delayed expansion – SomethingDark Jul 26 '16 at 11:05
  • @SomethingDark How would this be done, exactly? It'd make a good answer if it works. I could only see using delayed expansion to expand a single variable name, not a name including `%` and additional items. – Alyssa Haroldsen Jul 26 '16 at 12:17

1 Answers1

0

Use call set.

:: This code is for a batch file, as expansion works differently.
set x=%%systemroot%%\system32\config
:: '%x%' is now '%systemroot%\system32\config'
call set "y=%x%"

call will parse its code block twice, so call set "y=%x%" will first expand to set "y=%systemroot%\system32\config", and then when the set is called, it is expanded (on my system) to set "y=C:\Windows\system32\config".

Community
  • 1
  • 1
Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35