0

I am trying to write a script that among other things tries to create a shortcut up on a user's desktop. The problem I discovered was that some users don't use the standard location for their desktop location.

I implemented the following code to read the value from the Registry:

for /f "usebackq tokens=3*" %%D IN (`reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) do set DESKTOP=%%D

CALL SET REGISTRYDESKTOP=%DESKTOP%

echo Desktop is located in "%REGISTRYDESKTOP%"

When I looked at the Registry, I see the following: enter image description here

When the code runs, I get the following response: enter image description here

I imagine I am missing something simple, what am I doing wrong here?

Regards,

Steve

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Try ```@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') Do @Set "DESKTOP=%%~H"``` instead – Compo Mar 06 '23 at 19:42
  • Using `call set` will also work as in `for /f "tokens=2*" %%a in ('%__APPDIR__%reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop" 2^>^&1') do call set "deskFold=%%b`. – Qwerty Mar 06 '23 at 19:44
  • You don't have to re-invent this, look [here](https://stackoverflow.com/a/58516212/3074564). – Mofi Mar 07 '23 at 18:02
  • Okay, I keep digging on this, and what I am finding is that the new location for the profile is "C:\Users\\OneDrive - \Desktop" I think the problem is that I have spaces in the path name. what my code is returning is "C:\Users\\OneDrive" I have tried quoting the whole thing, but quotes already there are interfering with eachother. Syntax help anyone? – Stephen Lundy Mar 07 '23 at 18:58

1 Answers1

0

Thanks for all your help.

The final answer did turn out to be rather simple. First I needed to update the tokens clause of where I retrieved the string from the registry:

for /f "usebackq tokens=*" %%D IN (`reg query 
    "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
    /v Desktop`) do set DESKTOP=%%D

and second I needed to update the call set line:

CALL SET "REGISTRYDESKTOP=%DESKTOP:*REG_EXPAND_SZ =%"

Hope this helps anyone else facing this issue.

greybeard
  • 2,249
  • 8
  • 30
  • 66