views:

1352

answers:

7

I'm writing a Windows batch file and want to copy something to the desktop. I think I can use this:

%UserProfile%\Desktop\

However, I'm thinking, that's probably only going to work on an English OS. Is there a way I can do this in a batch file that will work on any internationalized version?

UPDATE

I tried the following batch file:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

And got this output:

S:\>REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
    Desktop    REG_EXPAND_SZ    %USERPROFILE%\Desktop


S:\>FOR /F "usebackq tokens=3 skip=4" %i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folder
s" /v Desktop`) DO SET DESKTOPDIR=%i

S:\>FOR /F "usebackq delims=" %i in (`ECHO ECHO is on.`) DO SET DESKTOPDIR=%i

S:\>SET DESKTOPDIR=ECHO is on.

S:\>ECHO ECHO is on.
ECHO is on.
+3  A: 

Not only would that not work for an International version of Windows, it would fail if the user had edited the Registry to make their Desktop folder reside somewhere else. You can query the Registry for the file location using the REG command:

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop

To get this into a variable use something like this:

FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Dave Webb
For me, on Windows 7, that returns `Desktop REG_EXPAND_SZ %USERPROFILE%\Desktop` which is correct but probably won't help the OP in his batch file, as he probably won't be able to parse the environment variable. Or would he just have to use it and it would parse itself?
Pekka
Have added some code to parse the output of `REG.EXE` to the answer.
Dave Webb
Thanks for the tip Dave. I'm not quite there yet though - think I'm missing something. I've updated the question to show where I've got to. If you've got any more advice, it would be appreciated.
Scott Langham
Keep in mind that querying this from the registry is discouraged. Kb's VBScript solution is therefore the one that should be used to ensure that this works.
Joey
A: 

@Dave Webb's answer is probably the way to go. The only other thing I can think of are the CSIDLs:

CSIDL_DESKTOPDIRECTORY

The file system directory used to physically store file objects on the desktop (which should not be confused with the desktop folder itself). A typical path is C:\Documents and Settings\username\Desktop.

I have no idea how to get hold of those from the command line, though.

Pekka
+9  A: 

To be safe, you should use the proper APIs in Powershell (or VBScript)
Using PowerShell:

[Environment]::GetFolderPath("Desktop")

Copy something using Powershell:

Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))

Useful Powershell link.

Here is a VBScript-example to get the desktop path:

dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring
Kb
+1 for using the proper APIs for this. The registry shouldn't be used. (Anyone not getting this: Read Raymond Chen's blog. Yes, completely.) :-)
Joey
A: 

This is not a solution but I hope it helps: This comes close except that when the KEY = %userprofile%\desktop the copy fails even though zdesktop=%userprofile%\desktop. I think because the embedded %userprofile% is not getting translated.

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop>z.out
for /f "tokens=3 skip=4" %%t in (z.out) do set zdesktop=%%t
copy myicon %zdesktop%
set zdesktop=
del z.out

So it sucessfully parses out the REG key but if the key contains an embedded %var% it doesn't get translated during the copy command.

fupsduck
A: 

you could also open a DOS command prompt and execute the set command.

This will give you an idea what environment variables are available on your system.

E.g. - since you where specifically asking for a non-english Windows - heres is an example of my own German Edition (Window7-64bit) :

set > env.txt
type env.txt

ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\SOF\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=VMSOF
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\SOF
LOCALAPPDATA=C:\Users\SOF\AppData\Local
LOGONSERVER=\\VMSOF
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\CMake 2.8\bin;C:\Program Files (x86)\emacs-22.3\bin;C:\Program Files (x86)\GnuWin32\bin;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=AMD64 Family 15 Model 67 Stepping 3, AuthenticAMD
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=4303
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\SOF\AppData\Local\Temp
TMP=C:\Users\SOF\AppData\Local\Temp
USERDOMAIN=VMSOF
USERNAME=SOF
USERPROFILE=C:\Users\SOF
VBOX_INSTALL_PATH=C:\Program Files\Sun\VirtualBox\
VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
windir=C:\Windows

Windows 7 is pretty boring, since Microsoft introduced filesystem virtualization in Windows Vista *precisely* to avoid problems like the one the OP is facing. IOW: *Every* language version of Windows 7 will have the desktop in `C:\Users\%USERNAME%\Desktop` even though if you actually open up Explorer it will *pretend* to live in `C:\Benutzer` (for a German system).
Jörg W Mittag
Off-Topic: How can you have *that* account name and *not* have a picture associated with the account? :-)
Jörg W Mittag
Jörg: That's only halfway correct. You can easily move the Desktop folder somewhere else. This doesn't leave a junction behind to point to the new place. Also the user's profiles doesn't need to be under `C:\Users`. It can be anywhere; again, no junction to point there. Those are just new defaults for paths as well as some junction points to keep programs with hard-coded legacy paths happy. Well, and to not use localized names in the file system which really makes no sense in a MUI environment.
Joey
Vokuhila: I seriously wonder where you found a *DOS* prompt on a 64-bit Windows :-). cmd.exe is *not* DOS.
Joey
+1  A: 

I found that the best solution is to use a vbscript together with the batch file.

Here is the batch file:

@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%

Here is findDesktop.vbs file:

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)

There may be other solutions but I personally find this one less hackish.

I tested this on an English PC and also a French PC - it seems to work (Windows XP).

HTH,

Iulian Şerbănoiu

Iulian Şerbănoiu
+1  A: 

KB's answer to use [Environment]::GetFolderPath("Desktop") is obviously the official Windows API for doing this.

However, if you're working interactively at the prompt, or just want something that works on your machine, the tilda (~) character refers to the current user's home folder. So ~/desktop is the user's desktop folder.

Saqib
@Saqib: This is right. +1 And I beieve the desktop will always be "desktop"
Kb