如何在批处理文件中扩展 REG_EXPAND_SZ?

How do you expand a REG_EXPAND_SZ in a batch file?

我希望能够扩展使用 reg query <KEY> /v <VALUE> 检索到的 REG_EXPAND_SZ 的值。

如何展开包含环境变量(用%包围)的环境变量? 比如我想把x里面的%systemroot%\system32\config展开成C:\Windows\system32\config.

使用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%"

callparse its code block twice,因此 call set "y=%x%" 将首先扩展为 set "y=%systemroot%\system32\config",然后在调用集合时,它会扩展(在我的系统上)为set "y=C:\Windows\system32\config".