Search code examples
batch-fileregistryenvironment-variables

How do you expand a REG_EXPAND_SZ in a batch file?


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.


Solution

  • 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".