0

I'm struggling in Windows 10 with get desktop path of current user with this script:

ECHO OFF
FOR /F "skip=2 tokens=2,*" %%A IN ('reg.exe query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"') DO SET "UserDesktopPath=%%B"
            
ECHO  UserDesktopPath is:  %UserDesktopPath%
DIR %UserDesktopPath%
PAUSE

Basically its works when registry key have custom value (e.g. C:\Test\Desktop): Custom Desktop Path

But don't work, when registry key have default value for desktop like: %USERPROFILE%\Desktop

Then my script will have this output:

Default Desktop Path

Any ideas why cmd script can't read system variable %USERPROFILE%?

MikeZetPL
  • 13
  • 2
  • If a value may contain a SPACE character, you need to "quote the string", i.e. `DIR "%UserDesktopPath%"` – Magoo Mar 16 '22 at 18:35
  • If the registry value returns as a REG_EXPAND_SZ type, your value would require another level of expansion, to prevent it being read as a literal string. – Compo Mar 16 '22 at 18:37
  • Thanks very much for this tips! I started to look in another way and found another possible query of reg and its works: **** FOR /F tokens^=3 %%I IN ('reg.exe query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"^|find/i "Desktop"') DO – MikeZetPL Mar 16 '22 at 19:27

0 Answers0