Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the environment variable for the path to the desktop?

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. 
like image 755
Scott Langham Avatar asked Jan 04 '10 16:01

Scott Langham


People also ask

What is your PATH environment variable?

The PATH environment variable is an important security control. It specifies the directories to be searched to find a command. The default systemwide PATH value is specified in the /etc/profile file, and each user normally has a PATH value in the user's $HOME/. profile file.

What is the path for Desktop?

The location of the Desktop folder in most versions of Windows is %USERPROFILE%\Desktop , which for most users becomes C:\Users\YOURUSERNAME\Desktop .

How do I find my computer's Environment Variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.


2 Answers

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")) 

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 
like image 50
Kb. Avatar answered Oct 08 '22 17:10

Kb.


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).

like image 43
2 revs Avatar answered Oct 08 '22 17:10

2 revs