1

I use the following registry import for a right-click context menu entry, NewTextFileAndOpen, (in Notepad).

Right click context menu

Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):22,00,43,00,3a,00,5c,00,74,00,6f,00,6f,00,6c,00,73,00,5c,00,5f,\
  00,49,00,43,00,4f,00,5c,00,6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,\
  69,00,63,00,6f,00,22,00,00,00

[HKEY_CURRENT_USER\Software\Classes\Directory\shell\NewTextFileAndOpen\command]
@="cmd /c cd %V & echo.>NewTextFile.txt & start NewTextFile.txt"


[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen]
@="New Text File and Open"
"Icon"=hex(2):22,00,43,00,3a,00,5c,00,74,00,6f,00,6f,00,6c,00,73,00,5c,00,5f,\
  00,49,00,43,00,4f,00,5c,00,6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,\
  69,00,63,00,6f,00,22,00,00,00

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\NewTextFileAndOpen\command]
@="cmd /c cd %V & echo.>NewTextFile.txt & start NewTextFile.txt"

When I open again the notepad, using the right-click context menu, NewTextFileAndOpen, any new edits are always saved in NewTextFile.txt. I want to save in a text file with another name.

I ask for your help in saving a new auto name:

NewTextFile.txt
NewTextFile (1).txt
NewTextFile (2).txt
NewTextFile (3).txt
NewTextFile (4).txt

I tried to modify the following command line :

@="cmd /c cd %V & echo.>NewTextFile.txt & start NewTextFile.txt"
Compo
  • 36,585
  • 5
  • 27
  • 39
karimovv
  • 11
  • 2

1 Answers1

0

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143