I'm trying to use the value of an environment variable I capture from a registry key in a batch script, but for whatever reason unknown to me, the variable does not get expanded.
However, I can get it resolved if I do not capture it from the registry.
This is the script:
ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION
CLS
FOR /F "tokens=2*" %%a IN ('REG QUERY "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop" 2^>^&1^|find "REG_"') DO @SET SHORTCUTDESTINATION=%%b
ECHO %SHORTCUTDESTINATION%
PAUSE
SET VALUEFROMREGISTRY=%SHORTCUTDESTINATION%
ECHO %VALUEFROMREGISTRY%
PAUSE
ECHO %USERPROFILE%
PAUSE
This is the outcome - Results within the red rectangle are not resolved; in green, they are.
What am I missing?

REG_EXPAND_SZ. As such its value should contain a variable for expanding. In order to do that you would need to expand that value byEchoing the value within another cmd.exe instance, through anotherFor /Fcommand. For example:@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /V Desktop 2^>NUL ^| %SystemRoot%\System32\find.exe /I "REG_"') Do @For /F "Delims=" %%I In ('Echo "%%~H"') Do @Set "SHORTCUTDESTINATION=%%~I".