6
C:\Documents and Settings\Administrator\Desktop

I don't want to type the above each time to refer to a file on the desktop

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
user198729
  • 61,774
  • 108
  • 250
  • 348
  • Possible duplicate of [path of user desktop in batch files](https://stackoverflow.com/questions/18629768/path-of-user-desktop-in-batch-files) – StayOnTarget Nov 12 '19 at 15:20

4 Answers4

9

You can use "%USERPROFILE%\Desktop" but I don't know from which version of Windows it is built in.

If your want the real folder where Desktop is located then use this code in the bach

for /F "skip=2 tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do set DesktopFolder="%%a"

This requires the reg.exe to be available (again, I don't know from which version of Window it is there) and it will set the DesktopFolder variable to the path of the Desktop.

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
  • 3
    `%USERPROFILE%` is available already on XP (and probably earlier, too). – Vlad Apr 07 '10 at 09:41
  • It says can't find `C:\Documents` – user198729 Apr 07 '10 at 09:46
  • 2
    Note that this won't work for localized Windows versions where the `Desktop` folder has another name. – Helen Apr 07 '10 at 09:46
  • 1
    @user198729: You need to place quotes around `%USERPROFILE%\Desktop`, because the path contains spaces. – Helen Apr 07 '10 at 09:47
  • It says:`File or directory "C:\Documents and Settings\Administrator\Desktop\logo1w.png" is not found` – user198729 Apr 07 '10 at 09:50
  • 1
    @user19.. does the file (the png) really exists, what are you trying to-do what is the full command? – Shay Erlichmen Apr 07 '10 at 09:51
  • @Shay: Also, the desktop folder isn't necessarily located inside %USERPROFILE% - it can be moved to an arbitrary location, even to another drive (see http://support.microsoft.com/kb/242557) – Helen Apr 07 '10 at 09:54
  • I tried both `ls "%USERPROFILE%\Desktop"\logo1w.png` and ls "%USERPROFILE%\Desktop\logo1w.png",the file exists on desktop. – user198729 Apr 07 '10 at 09:55
  • @Helen,is there a **single** environment variable to locate desktop? – user198729 Apr 07 '10 at 09:58
  • 2
    @user198729: There is no direct environment variable for this... this is the limit of .bat/.cmd. Other scripting options (PowerShell and Windows Scripting Host for VBS and JS) offer a richer environment with objects/properties with far more details context. – Richard Apr 07 '10 at 10:17
  • @user198729: To determine whether the problem is in the file or the directory: go to %userprofile%\desktop in explorer, or type cd "%userprofile%\desktop" in the command console, running as the user which the batch file will be run as, and see if it takes you to the right place (ie, a folder with that file in it). – Anonymous Apr 07 '10 at 10:20
  • XP is the first version that has reg.exe out of the box (Pre XP only had it in the reskit) As far as the shell key goes, everyone should read http://blogs.msdn.com/oldnewthing/archive/2003/11/03/55532.aspx – Anders Apr 07 '10 at 13:06
  • The file could also be on the All Users desktop. – aphoria Apr 07 '10 at 13:26
  • @Anders typical, tell you don't use that but don't tell you how for example you suppose to get the value in a batch file (other than creating a console application that will do that). – Shay Erlichmen Apr 07 '10 at 14:14
  • 1
    Note that the registry key is *also* the wrong way to do it. You have to use the Known Folder API to get the path to such folders, otherwise the result might be wrong. And yes, you can't get that directly from a batch file. – Joey Apr 11 '10 at 08:41
7

The hybrid of Anders can be a bit more simple and readable, with the method described here hybrid scripting by Tom Lavedas.

@if (@X)==(@Y) @goto :Dummy @end/* Batch part

@echo off
SETLOCAL ENABLEEXTENSIONS
for /f "delims=" %%x in ('cscript //E:JScript //nologo "%~f0"') do set desk=%%x
echo desktop path is %desk%
goto :EOF

***** Now JScript begins *****/
WScript.Echo(WScript.CreateObject("Shell.Application").Namespace(16).Self.Path);
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 2
    `WScript.Echo(WScript.CreateObject("WScript.Shell").SpecialFolders("Desktop"));` is shorter, and looks more readable to me – Rob W Jun 10 '12 at 13:48
2

If you absolutely need to have a batch file, but want to use the power of windows scripting host, you might want to try a WSH/batch hybrid

Batch/WSH hybrid:

@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
for /f "delims=" %%x in ('cscript //E:JScript //nologo "%~f0"') do set desk=%%x
echo desktop path is %desk%
@goto :EOF
@end @ELSE
WScript.Echo(WScript.CreateObject("Shell.Application").Namespace(16).Self.Path);
@end

See ShellSpecialFolderConstants if you need to get the path of some other shell folder

Anders
  • 97,548
  • 12
  • 110
  • 164
  • @Johannes Rössel: Why not make your own answer? He asked for a batch file and that is what I'm trying to give him, with no external files. – Anders Apr 11 '10 at 12:22
  • Ah, sorry. Didn't read your link well enough. I merely tried to provide code since a single sentence is usually not very helpful. I'd spin the WSH stuff into a separate file anyway, though, since that's pretty much a mess to read. It also has no advantages over a separate file since it still depends on the WSH not being disabled through GP. Once your batch files get larger you probably want to go for readability. Note also that that ugly *@name* stuff only works if I have a comment here; it's not a general means of addressing anyone on this site. – Joey Apr 11 '10 at 14:06
  • @Johannes Rössel: How is 1 single file vs 2 separate files not an advantage? As far as @name goes, I know, it was the only option I had. – Anders Apr 11 '10 at 14:31
  • 1
    You can just as easily write a JS script into another file from your batch and call it. That way you can distribute a single file but get the benefit of not having to jump through syntactic hoops that make a single file a polyglot. – Joey Apr 11 '10 at 16:37
  • @Joey: In my opinion one file is better than two, even better than a temporary file, but you are right, this type of hybrid is cruel to read – jeb Apr 13 '11 at 22:07
1
set UserDesktop=%UserProfile%\Desktop

if exist %Public% (
    set SharedDesktop=%Public%\Desktop
) else (
    set SharedDesktop=%AllUsersProfile%\Desktop
)

So now you can use the Local Variables

%UserDesktop% and %SharedDesktop%

SharedDesktop first case is for Vista and above the else is for XP

ps: before using these variables you should quote then "%UserDesktop%" because Username must have spaces, like ...\Bill Gates\... or \Documents and settings\...

Vitim.us
  • 20,746
  • 15
  • 92
  • 109