Some people tend to save their documents and files on a separate drive. This allows the flexibility of flattening the Windows/OS partition at any time, without losing any data. This is one reason I have never been a fan of using the %USERPROFILE% folders (eg. C:Users<UserName> on Vista or C:Documents and Settings<UserName> on XP).
It turns out, though, that you can redirect these ‘special’ shell folders easily on Vista, or with the Tweak UI PowerToy for XP (or hacking up the registry). These folders include things like “My Pictures,” “My Music,” “Favorites,” or even your “Desktop” folder. On Vista, for instance, when you right-click the Desktop folder and select Properties, you get the dialog on the right.
If you’ve done this, obviously it’s not good enough to assume that your desktop is in %USERPROFILE%Desktop. You’ve got to open up the registry to find the location of these “User Shell Folders”:
set OUTPUTDIR=%USERPROFILE%Desktop
set SHFOLDER_REGISTRY_KEY = "HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders"
for /f "tokens=2*" %%i in (
‘REG QUERY %SHFOLDER_REGISTRY_KEY% /v Desktop’
) do (
call set OUTPUTDIR=%%~j
)
Notes:
- We’re querying HKCU, so no elevation on Vista is required.
- Most of values underneath this key are of type REG_EXPAND_SZ. This is why we need to ‘call’ to expand the value.
- Bonus points if you can tell me why I start with token #2.
In some cases, you may also find that there is a “DesktopDirectory” value as well as a “Desktop” value underneath that key. I cannot eloquently explain the difference, but perhaps you may be able to interpret Wikipedia’s explanation:
The "Desktop" virtual folder is not the same thing as the "Desktop" special folder. The Desktop virtual folder is the root of the Windows Shell namespace, which contains other virtual folders.
I believe that DesktopDirectory is the correct value to use, but sometimes it is not available. Most of the time the two values are equivalent.
I bet you someone on the shell team could explain this properly, if he hasn’t already. I’m sure there’s an interesting story behind it.