When I run the batch file (on a usb) with this code:
@echo off
cd desktop
type nul > Test.txt
It creates Test.txt on the usb instead of on the desktop. Is it possible to make the Test.txt
file created on the desktop instead of USB?
When I run the batch file (on a usb) with this code:
@echo off
cd desktop
type nul > Test.txt
It creates Test.txt on the usb instead of on the desktop. Is it possible to make the Test.txt
file created on the desktop instead of USB?
There are predefined Windows environment variables for each user account. One of this predefined environment variables is USERPROFILE
containing path to the user's profile directory.
The directory of the user's desktop is by default %USERPROFILE%\Desktop
as defined in Windows registry under key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
with value Desktop
of type REG_EXPAND_SZ
.
There is also key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
also with value Desktop
, but of type REG_SZ
with folder path of user's desktop with environment variable reference already expanded.
A user has the possibility to modify the folder path of any shell folder including desktop folder although most shell folder paths are kept as defined by default.
So a safe method to get path of user's desktop folder is:
@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 Desktop folder is: "%DesktopFolder%"
endlocal
The environment variable DesktopFolder
is first deleted if already existing by chance in local environment of the batch file.
Next by default not expanded registry value Desktop
of registry key User Shell Folders
is queried and if the registry key with this registry value exists with a non-empty string as expected, the folder path is assigned expanded to environment variable DesktopFolder
. In this case call
is used to expand the environment variable reference already before assigning the folder path to environment variable DesktopFolder
by double parsing this last part of the long command line. For security the type of registry value is also evaluated although this registry value should be always of type REG_EXPAND_SZ
.
It is very unlikely that first registry query is not successful on any Windows including Windows 2000, Windows XP and Windows Server 2003 and of course all newer versions of Windows. But if first registry query failed to determine the user's desktop folder, one more registry query is made for string value of Desktop
under key Shell Folders
which is by default of type REG_SZ
which means that registry value holds the directory path already expanded.
Last the environment variable DesktopFolder
is defined with default folder path using predefined environment variable UserProfile
for nearly 100% security if both registry queries failed unexpectedly.
So finally the environment variable DesktopFolder
holds folder path to user's desktop folder up to the line with command endlocal
.
The order for determination of Desktop
directory path is exactly the same as used by Windows itself with the same error handling in case of a Desktop
registry value is not existing at all which is very unlikely, but temporarily possible according to my tests with manual deletions of one or both registry values in various test cases.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
if /?
reg /?
and reg query /?
set /?
setlocal /?
Read the Microsoft article about using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg
command line with using a separate command process started in background with %ComSpec% /c
which means usually with C:\Windows\System32\cmd.exe /c
.
Try specifying the full path in the output line as:
@echo off
type nul > path\to\desktop\Test.txt