So I am making a batch file that I will put in the startup folder. I need to make a directory in the desktop directory of current user. I only know how to make a new directo
I would offer a method which retrieves the path from the registry:
@For /F "EOL=HTokens=2*" %%A In ('^""%__AppDir__%Reg.exe" Query^
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"^
/V Desktop^" 2^>NUL')Do @Call MD "%%B\New Directory" 2>NUL
The above is designed to create a directory named New Directory
, (change as required), on the current users desktop.
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 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 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 variables by using command CALL to force one more command line parsing by Windows command processor.
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:
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"
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 every create other files than shortcut files or directories 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.