3

I know how to set the title as the current directory in a regular batch file.

I want to do this for the cmd window that opens when you do an "open command window here" from a Windows folder (automatically).

I know you can run a batch file at the startup of any cmd and I've seen answers to get the directory name but trying to get the directory name only gives you the directly name where that autorun.cmd file is in, not the directory name which "open command window here" cds into.

It seems like at the time the autorun.cmd is invoked it doesn't yet have the information about the directory that will change to whichever the "open command window here" was invoked from. Is that the case? Or is there still a way to change the title automatically to the current directory?

enter image description here

2

3 Answers 3

3

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).

14
  • Thank you!!! Two things: In my case it was HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\cmd\command. And: It ignores spaces (It gives title "Folder" for "New Folder") Commented Nov 17, 2014 at 9:00
  • @laggingreflex: That's essentially the same path. HKCR is just a "shortcut". :) As for ignoring spaces: Yes, that's probably some leftover from old DOS times (as is the limitation to one dot).
    – Mario
    Commented Nov 17, 2014 at 9:04
  • Hmm. I tried it and it didn't work. Then I searched regedit for "cmd.exe /s /k pushd "%V"" and found a couple of entires, some of which I was unable to edit (some error). That was the one I finally edited and it worked. Maybe it's only for my case, I do have some setting enabled so I don't have to hold shift to get the "open command window". Commented Nov 17, 2014 at 9:07
  • To avoid having to hit the shift key, just go to the paths described in my answer and remove the Extended value under the cmd keys. Although it's indeed possible some other tool edited permissions and such to avoid further modifications/resets.
    – Mario
    Commented Nov 17, 2014 at 9:15
  • It is not only your case, special folders like Desktop etc. are handled by other keys in registry. And there's no need to use for command, cmd.exe /s /k "pushd ""%V"" & title %V" is enough. See this question: superuser.com/questions/414155/… (Earlier when writing my answer to your question I forgot that the question was already answered somwhere else)
    – MBu
    Commented Nov 17, 2014 at 9:19
1

Inspired by the question, I have added some fun functionality to the command line on my Windows: pseudocommands cdn, pushdn and popdn (trailing N = acronymous new or naming or even nonsens or whatever else) corresponding to cd, pushd and popd commands. Those pseudocommands help to keep my cmd window title parallel to the cmd current directory path as follows:

current directory    window title
------------------   --------------
X:\subpath\subfold   X:\ ..\subfold 
X:\folder            X:\folder
X:\                  X:\

for any drive X: and arbitrary depth of subpath.

Code example: cdn.bat placed to any folder explicit in the path environment variable.

@rem cdn.bat
@rem change directory (and drive) || abort script processing in case of bad success
@cd /D %* || @goto :eof
@rem eliminate (if any in %*) trailing backslashes, surrouding double-quotes
@rem and/or (combined) symbols to current, parent or root directory (., .., \)
@call :window_title "%CD%"
@rem or, to title window to bare folder name, use: 
@rem @for /F "tokens=*" %%G in ("%CD%") do @title %%~nG%%~xG
@goto :eof

:window_title
  @if "%~p1%~n1%~x1" == "\%~n1%~x1" (
    @rem window title to 'X:\folder' on highest-level path or to 'X:\' on drive root
    @title %~d1%~p1%~n1%~x1
  ) else (
    @rem window title to 'X:\...\folder' otherwise (i.e. nor root, nor highest-level)
    @title %~d1^\ ..^\%~n1%~x1
  )
  @exit /B

Pleasure sharing my delight.

0

This is now possible to keep up to date automatically after any command, without redefining 'cd' etc. See Change command prompt to only show current directory name

2
  • Welcome to Super User! As with your other answer (identical to this one), it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – bertieb
    Commented Oct 9, 2018 at 22:42
  • Also, the linked post was deleted, so this link points nowhere.
    – fixer1234
    Commented Oct 10, 2018 at 6:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .