Create a batch file with a name like NewTextFileAndOpen.cmd
or another name you like in the directory C:\Tools
or another directory of your choice with the following lines:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" exit /B
set "NewFileName=%~1\NewTextFile.txt"
if not exist "%NewFileName%" goto CreateAndOpen
set "FileNumber=1"
:FindFreeNumber
set "NewFileName=%~1\NewTextFile (%FileNumber%).txt"
if exist "%NewFileName%" set /A "FileNumber+=1" & goto FindFreeNumber
:CreateAndOpen
echo(>"%NewFileName%"
start "" /D "%~1" "%NewFileName%"
endlocal
This batch file requires being started with a folder path as first argument string. It does nothing and immediately exits if it is started without any argument string.
There is no verification made if the passed argument string references really an existing directory, but that could be changed for safe usage of the batch file from within a command prompt window by inserting after the first IF condition the command line:
for %%I in ("%~1") do if not exist "%%~fI\" do echo No directory: "%~1"& pause & exit /B
The registry file with name NewTextFileAndOpen.reg
stored also in C:\Tools
should contain the lines:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,69,00,\
6d,00,61,00,67,00,65,00,72,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,2c,00,2d,\
00,31,00,30,00,32,00,00,00
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,69,00,\
6d,00,61,00,67,00,65,00,72,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,2c,00,2d,\
00,31,00,30,00,32,00,00,00
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
The registry value Icon
is in this case of type REG_EXPAND_SZ
with the value:
%SystemRoot%\system32\imageres.dll,-102
There can be used also the following registry file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,69,00,\
6d,00,61,00,67,00,65,00,72,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,2c,00,2d,\
00,31,00,30,00,32,00,00,00
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,69,00,\
6d,00,61,00,67,00,65,00,72,00,65,00,73,00,2e,00,64,00,6c,00,6c,00,2c,00,2d,\
00,31,00,30,00,32,00,00,00
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
The registry value Icon
of type REG_EXPAND_SZ
has with this registry file the value:
%SystemRoot%\notepad.exe,-2
Or there is used the following registry file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"="\"C:\\Tools\\_ICO\\notepad.ico\""
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"="\"C:\\Tools\\_ICO\\notepad.ico\""
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen\command]
@="C:\\Windows\\System32\\cmd.exe /D /S /C \"\"C:\\Tools\\NewTextFileAndOpen.cmd\" \"%V\"\""
The registry value Icon
is of type REG_SZ
with the value:
"C:\Tools\_ICO\notepad.ico"
Please modify the path of cmd.exe
if Windows is installed on a different drive like D:\Windows
. It is better to use the fully qualified file name of cmd.exe
instead of just cmd
. It is better using a registry value of type REG_SZ
and not of REG_EXPAND_SZ
for the command to execute.
The registry file can be imported from within a command prompt window with execution of the command line:
reg.exe IMPORT "C:\Tools\NewTextFileAndOpen.reg"
There are no elevated privileges of a local administrator required for the registry import using this command line.
The batch file and the command line imported into the registry are designed for working also for directories like:
C:\Temp\Development & Test(!) 100%
\\Server\Share\Folder\SubFolder
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
... explains %~1
... first argument string with surrounding "
removed.
cmd /?
echo /?
endlocal /?
exit /?
goto /?
if /?
pause /?
set /?
setlocal /?
start /?
See also: