14

I am doing a simple batch file to copy folders and files located under the same path of the batch files to the desktop. I can easily get the path where the batch file is located using

%~dp0

but I want to know how to get the path of the user's desktop (I am using Widows 7 Ultimate)

Any suggestions?

1
  • You must specify the version of Windows (IF you are under windows), the question (without OS version) do not have a unique answer. – gmo Sep 5 '13 at 7:38
3

I think this one should be ok too

%systemdrive%\Documents and Settings\All Users\Desktop

Regards

3
  • 1
    @Jack_111 - This works on XP, but does not work on Win 7. You really should accept the Mali answer, as it is more general. Your solution is also good, but not everyone has access to the registry (I don't at my workplace) – dbenham Sep 5 '13 at 11:29
  • This actually does work in Windows 7. For backwards compatibility, "Documents and Settings" and associated directories are mirrored so older installers will work correctly. (tested on windows 7 x64) – Joe Lyga Sep 11 '14 at 14:55
  • 1
    @Jack_111 the problem with this solution is that it puts the files on the desktop accessible for all users. So everyone using the computer can access them, not only the user running the script. Where as Malis answer does exactly what you asked: give you the location of the current user's desktop. – Ichixgo Apr 7 '16 at 12:55
73

I suppose you're under windows environnement, so %USERPROFILE%\Desktop should be ok

2
  • 1
    The first Answer I have posted I think it is more general and it gives the desktop path in any case (The desktop is not in the user profile) and your answer as well works and I have posted that after in the normal and usual case that the desktop in the user profile (I have never seen a desktop not in the user profile) – Jack_111 Sep 5 '13 at 7:24
  • Doesn't work on my work laptop (Windows 7). kgimpel's solution outputs the right one (D:\Users\%USERNAME%\Desktop). Sounds like %USERPROFILE% can be inacurrate on some contexts. – Amessihel Apr 27 '17 at 14:37
20

This is the location of the current users desktop:

%userprofile%\desktop
1
  • 1
    Does not work if the desktop has been remapped somewhere else. – Jarryd Feb 23 '17 at 5:32
18

If Desktop locations in a different folder only this will be correct answer:

Batch string:

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

V2 (Works with spaces) 4.10.2016:

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

If you have non-ASCII symbols, you also need to convert ANSI encoding to OEM, example for cyrillic:

CHCP 1251 >Nul
for /f "usebackq tokens=1,2,*" %%B IN (`reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) do set DESKTOP=%%D
CHCP 866 >Nul
for /f "delims=" %%i IN ('echo %DESKTOP%') do set DESKTOP=%%i

Then just use:

echo %DESKTOP%
1
  • Great, thanks for the fix, +1 to you. I've also added to your answer a little info and code for encoding handling, hope you don't mind. – user Oct 5 '16 at 5:19
3

I found the answer

Regedit /e /a dd.txt       

"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

find /i  dd.txt  "Desktop" >d.txt
For /F  "tokens=2 delims==" %%a in (d.txt) do set mydesktop=%%a
2
  • or simpler one %userprofile%\desktop if the desktop is in the user profile and that's the common and normal case – Jack_111 Sep 5 '13 at 7:22
  • 2
    That answer was already given by Mali. Don't try to be the hero here ;-) – script'n'code Apr 3 '17 at 23:42
0

Environ("USERPROFILE") & "\Desktop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.