I'd try the following line:
for %a in (.) do title %~na
Or in a batch file you'd escape the %
s once:
for %%a in (.) do title %%~na
The for...do
loop is just there to get the current path into the variable since you can't use the ~
operator with environment variables like (%cd%
).
However, as you noticed, this won't work for the "Command Prompt here", since this is executed before the directory is set.
To circumvent this, you'll essentially have to modify the command line being called whenever you use this functionality.
This is controlled by two variables in the Registry, both being sub keys of HKEY_CLASSES_ROOT\Directory
:
HKEY_CLASSES_ROOT\Directory\shell\cmd\command
: This key defines the command to be run when you Shift + rightclick a directory/folder icon.
HKEY_CLASSES_ROOT\Directory\Background\shell\cmd\command
: This key defines the command to be run when you shift + rightclick somewhere in an open Explorer window.
By default, both of these default values are set to cmd.exe /s /k pushd "%V"
, which will open a command window and change directory to the parameter passed as %V
.
So for this to work, you'll have to edit those two default values and append the command from above, slightly modified. Simply set both default values to this:
cmd.exe /s /k "pushd ""%V"" && for %%A in (%V) do @title %%~nA"
Note the double quotes to properly escape them, since everything is enclosed in a single pair of quotes to group everything for cmd.exe's /k
parameter. The @
in there will hide the command from showing inside the command window.
This works for me, but there's one little quirks involved: If your directory name contains more than one dot, like one.two.three
, this will name the title one.two
only.
Also keep in mind that the title will not update when you CD to another directory. Getting this to work would be quite a bit more trickier (or maybe even impossible; didn't try).
Is it actually important that the title always shows the current directory or is it enough to show the startup dir? – Mario – 2014-11-17T08:49:35.140
possible duplicate of cmd.exe customizing the "Open Command Prompt here" shell extension
– MBu – 2014-11-17T08:50:06.477