Home > OS >  Variable value extracted from registry is not expanded
Variable value extracted from registry is not expanded

Time:12-13

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.

batch

What am I missing?

CodePudding user response:

You can use call echo and call set to force cmd to expand the %userprofile% system variable.

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%
call echo %SHORTCUTDESTINATION%
PAUSE

SET VALUEFROMREGISTRY=%SHORTCUTDESTINATION%
call SET VALUEFROMREGISTRYCALL=%SHORTCUTDESTINATION%

ECHO %VALUEFROMREGISTRY%
ECHO %VALUEFROMREGISTRYCALL%
PAUSE
ECHO %USERPROFILE%
PAUSE
  • Related