Reputation: 891
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?
Upvotes: 19
Views: 120349
Reputation: 25
I reccomend to use this script:
@echo off
if exist "%userprofile%\OneDrive\Desktop" (chdir %userprofile%\OneDrive\Desktop) else (chdir %userprofile%\Desktop)
If someone installed OneDrive on windows setup, their desktop will probably be located under OneDrive folder. Else it will be User\Desktop.
Upvotes: 0
Reputation: 3345
Cleanest solution just for Desktop:
for /f "tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do set DESKTOP=%%b
echo %DESKTOP%
tokens=Type (2), then remaining Data (*) so location whitespace isn't tokenized. (spaces are allowed)
But if you're looking for any registry value which may not exist:
@echo off
set SUBKEY=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
set VALUE_NAME=Desktop
for /f "tokens=*" %%a in ('reg query "%SUBKEY%" /v "%VALUE_NAME%" 2^>nul') do set LINE=%%a
if defined LINE goto else
echo "%VALUE_NAME%" value not found.
goto end
:else
for /f "tokens=2,3 delims=#" %%a in ("%LINE: =#%") do (
echo Key: %SUBKEY%
echo Name: %VALUE_NAME%
echo Type: %%a
echo Data: %%b
)
:end
echo[
pause
tokens=*
gets the whole line (Name + Type + Data) which are separated by 4 spaces
%LINE: =#%
replaces the 4 spaces with hashtags so we can deliminate the tokens properly
tokens=2,3
gets us the Type and Data fields which get put into %%a
and %%b
If you just want the Data you can do tokens=3
and it'll be in %%a
. (since it's the only token)
Of all the examples i've been looking at all over the web, i haven't seen anyone else collecting the whole line and swapping the 4 spaces so they can tokenize properly. They all let it split at every whitespace, which can exist at both the Name and the Data fields. Of course if you're just after a specific value however (like "Desktop") you know the Type will be the 2nd token, and the remaining Data (*) is the last token. If you wanted a one liner for "My Pictures", you'd do token=3*
instead. ("My" "Pictures" "Type" (3) "Data" (*))
Also the 2^>nul
simply prevents the reg query command telling us if the value doesn't exist.
Upvotes: 2
Reputation: 49127
The default for the Windows desktop directory of current user is defined with %USERPROFILE%\Desktop
. USERPROFILE
is one of the predefined Windows environment variables.
So it would be possible to use just the following folder path:
"%UserProfile%\Desktop"
But it would be better to get the desktop directory path from Windows registry instead of using simply the default. There are two registry keys containing a string value with name Desktop
with the path to user's desktop directory:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
This registry key contains several string values usually of type REG_EXPAND_SZ
which define the paths to the various shell folders defined for the current user account. The shell folders contain usually an environment variable reference like %USERPROFILE%
which is the reason for the type REG_EXPAND_SZ
which means the string value must be additionally expanded to get absolute path to the shell folder. The batch file below expands the environment variable reference by using the command CALL to force one more command line parsing by Windows command processor.
For example, the command set "DesktopFolder=%%~K"
becomes first on execution of for
the command set "DesktopFolder=%USERPROFILE%\Desktop"
. This command is parsed a second time by cmd.exe
to something like set "DesktopFolder=C:\Users\UserName\Desktop"
because of the command call
before really executing the command set
to define the environment variable DesktopFolder
with the real absolute folder name read from the Windows registry.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
This registry key contains nearly the same string values as the registry key above, but the string values are usually of type REG_SZ
. This registry key is for downwards compatibility for applications not supporting the other registry key with the string values with environment variable references.
It is possible that a shell folder is defined only in one of the two registry keys. For example on Windows XP the string values Administrative Tools
, CD Burning
, Fonts
and Recent
exist only under registry key Shell Folders
and do not exist under key User Shell Folders
.
Information added by Compo with comments below my similar answer on:
How to create a directory in the user's desktop directory?
Windows itself uses by default the string values defined under key User Shell Folders
and uses a string value defined under key Shell Folders
only if not existing under key User Shell Folders
.
Windows does not propagate a modification on a string value under key User Shell Folders
to the string value with same name under key Shell Folders
if a user or a program modifies directly in registry a string value under key User Shell Folders
without making appropriate change to key with same name under key Shell Folders
.
So in case of Desktop
in User Shell Folders
contains a different directory path than Desktop
in Shell Folders
, Windows uses the path defined with Desktop
in User Shell Folders
.
A user has the freedom to change any folder to whatever the user wants. But the user must take care to change a string value in both registry keys on existing twice. Some of the shell folders can be easily modified via an option on graphical user interface of Windows or a Windows application like the Downloads
shell folder.
See also the Microsoft documentations for Known Folders and KNOWNFOLDERID and the other documentation pages referenced on these pages as well as the documentation about Application Registration.
Here is a batch file which gets the user's desktop directory from Windows registry as much safe as possible.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DesktopFolder="
for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder set "DesktopFolder=\"
if "%DesktopFolder:~-1%" == "\" set "DesktopFolder=%DesktopFolder:~0,-1%"
if not defined DesktopFolder set "DesktopFolder=%UserProfile%\Desktop"
echo User's desktop folder is: %DesktopFolder%
endlocal
This batch file works even on Windows XP on which reg.exe
outputs the results of the query different to reg.exe
of Windows Vista and newer Windows versions.
See Microsoft article about Using command redirection operators for an explanation of 2>nul
which redirects the error message output by command MD on directory already existing to handle STDERR to the device NUL to suppress this error message.
However, the user's desktop directory should contain only shortcut files (*.lnk
files) and the files and directories created by the user on the desktop. No program should create ever other files than shortcut files in the user's desktop directory. Microsoft defined several other shell folders for applications like APPDATA
(application data) or LOCALAPPDATA
(local application data) for applications.
Some additional facts about handling of string value Desktop
under the keys User Shell Folders
and Shell Folders
by Windows as observed with Windows XP SP3 x86 with always restarting Windows after making a change in registry hive of current user:
A change of the path string of the string value Desktop
under the key User Shell Folders
for example from %USERPROFILE%\Desktop
to %USERPROFILE%\MyDesktop
and of course creation of the directory %USERPROFILE%\MyDesktop
changes the Windows desktop directory to custom %USERPROFILE%\MyDesktop
on next log on and the string value of Desktop
under key Shell Folders
is adapted by Windows on next restart. It was not tested by me if Desktop
under the key Shell Folders
is adapted also on just doing a log off and log on. It is definitely better to change both Desktop
string values at the same time to change the desktop directory permanently to a directory different from default %USERPROFILE%\Desktop
.
A removed or renamed string value Desktop
under the key User Shell Folders
is never recreated by Windows. So it is possible that this string value does not exist if Desktop
under the key User Shell Folders
was by mistake once deleted or renamed or the registry file is partly damaged with the result that this string value does not exist. A user would not notice that issue as the further tests below showed.
The string value Desktop
of type REG_SZ
under key Shell Folders
is always set to expanded path of %USERPROFILE%\Desktop
if string value Desktop
of type REG_EXPAND_SZ
under key User Shell Folders
does not exist at all. Windows creates also the directory %USERPROFILE%\Desktop
automatically if not existing in this error handling case
If the string value Desktop
of type REG_SZ
under key Shell Folders
and the string value Desktop
of type REG_EXPAND_SZ
under key User Shell Folders
are both deleted or renamed by a user or program, Windows creates on next start the string value Desktop
of type REG_SZ
under key Shell Folders
with expanded path of %USERPROFILE%\Desktop
and creates also the directory if not existing. The string value Desktop
of type REG_EXPAND_SZ
under key USer Shell Folders
is not recreated by Windows.
If the string value Desktop
of type REG_SZ
under key Shell Folders
exists with a different expanded path than %USERPROFILE%\Desktop
like expanded path of %USERPROFILE%\MyDesktop
and the string value Desktop
of type REG_EXPAND_SZ
under the key User Shell Folders
does not exit at all, Windows ignores the customized path of Desktop
of type REG_SZ
under the key Shell Folders
and sets the value to expanded path of %USERPROFILE%\Desktop
and creates additionally the directory %USERPROFILE%\Desktop
if not already existing. So it is not possible to use a customized desktop directory without having the customized desktop directory defined also with string value Desktop
of type REG_EXPAND_SZ
under the key User Shell Folders
.
I did not make tests with newer versions on Windows regarding to handling of Desktop
under the keys Shell Folders
and User Shell Folders
if one or both string values do not exist and/or have same or different directory paths and/or have a directory path different to default.
Upvotes: 1
Reputation: 41257
This is the location of the current users desktop:
%userprofile%\desktop
Upvotes: 24
Reputation: 602
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%
Upvotes: 25
Reputation: 163
I think this one should be ok too
%systemdrive%\Documents and Settings\All Users\Desktop
Regards
Upvotes: 3
Reputation: 2700
I suppose you're under windows environnement, so %USERPROFILE%\Desktop
should be ok
Upvotes: 85
Reputation: 891
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
Upvotes: 3