0

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?

Roxiun
  • 185
  • 2
  • 10

4 Answers4

1
%USERPROFILE%\Desktop\Test.txt
1957classic
  • 499
  • 1
  • 7
  • 12
1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • What does `setlocal EnableExtensions DisableDelayedExpansion` do? – Roxiun Jun 10 '19 at 04:28
  • @Roxiun Read [this answer](https://stackoverflow.com/a/38676582/3074564) for details about the commands __SETLOCAL__ and __ENDLOCAL__. By default command extensions are enabled as required for this batch file and delayed environment variable expansion is disabled which is also better here for example in case of `%UserProfile%` expands to a folder path containing a `!` because of user name contains an exclamation mark. Additionally the command `setlocal` sets up a new environment for this code. So __by default__ the commands `setlocal` and `endlocal` are not really needed here. – Mofi Jun 10 '19 at 09:50
  • @Roxiun I write my batch files always to be independent on Windows defaults. By default means it is possible that a Windows computer is configured by the user different to default. I design my batch files to work independent on environment defined outside of the batch file to make it possible to call the batch file also from other batch files which set up a different environment than default cmd environment. And the batch file should not effect anything in a calling batch file like modifying environment variables defined already by a calling batch file. – Mofi Jun 10 '19 at 09:55
0

Try specifying the full path in the output line as:

@echo off

    type nul > path\to\desktop\Test.txt
xendi
  • 2,332
  • 5
  • 40
  • 64
  • What happens if I don't know the desktop location. Eg. Use Batch file on PC and then laptop. Wouldn't the location chnage? – Roxiun Jun 05 '19 at 03:31
0

Or by using break:

break>"%userprofile%\desktop\Test.txt"
Gerhard
  • 22,678
  • 7
  • 27
  • 43