How to create a directory in the user's desktop directory?
Solution 1:
The default for the Windows desktop directory is defined with %USERPROFILE%\Desktop
. USERPROFILE
is one of the predefined Windows environment variables.
So it would be possible to use just:
md "%UserProfile%\Desktop\NewDirectory" 2>nul
That would create a directory with name NewDirectory
on user's desktop as long as the user has not changed the default for the desktop directory. The command md
can be use with a full qualified directory path or a relative directory path. The help output on running in a command prompt md /?
explains that md
creates the entire directory tree to a directory not existing if command extensions are enabled as by default. See also the Microsoft documentation for naming files, paths, and namespaces.
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 typeREG_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 typeREG_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 commandset "DesktopFolder=%%~K"
becomes first on execution offor
the commandset "DesktopFolder=%USERPROFILE%\Desktop"
. This command is parsed a second time bycmd.exe
to something likeset "DesktopFolder=C:\Users\UserName\Desktop"
because of the commandcall
before really executing the commandset
to define the environment variableDesktopFolder
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 typeREG_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:
-
Windows itself uses by default the string values defined under key
User Shell Folders
and uses a string value defined under keyShell Folders
only if not existing under keyUser 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 keyShell Folders
if a user or a program modifies directly in registry a string value under keyUser Shell Folders
without making appropriate change to key with same name under keyShell Folders
.
So in case ofDesktop
inUser Shell Folders
contains a different directory path thanDesktop
inShell Folders
, Windows uses the path defined withDesktop
inUser 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"
md "%DesktopFolder%\NewDirectory" 2>nul
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 keyUser 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 ofDesktop
under keyShell Folders
is adapted by Windows on next restart. It was not tested by me ifDesktop
under the keyShell Folders
is adapted also on just doing a log off and log on. It is definitely better to change bothDesktop
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 keyUser Shell Folders
is never recreated by Windows. So it is possible that this string value does not exist ifDesktop
under the keyUser 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 typeREG_SZ
under keyShell Folders
is always set to expanded path of%USERPROFILE%\Desktop
if string valueDesktop
of typeREG_EXPAND_SZ
under keyUser 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 typeREG_SZ
under keyShell Folders
and the string valueDesktop
of typeREG_EXPAND_SZ
under keyUser Shell Folders
are both deleted or renamed by a user or program, Windows creates on next start the string valueDesktop
of typeREG_SZ
under keyShell Folders
with expanded path of%USERPROFILE%\Desktop
and creates also the directory if not existing. The string valueDesktop
of typeREG_EXPAND_SZ
under keyUSer Shell Folders
is not recreated by Windows. -
If the string value
Desktop
of typeREG_SZ
under keyShell Folders
exists with a different expanded path than%USERPROFILE%\Desktop
like expanded path of%USERPROFILE%\MyDesktop
and the string valueDesktop
of typeREG_EXPAND_SZ
under the keyUser Shell Folders
does not exit at all, Windows ignores the customized path ofDesktop
of typeREG_SZ
under the keyShell 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 valueDesktop
of typeREG_EXPAND_SZ
under the keyUser 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.