Set the "open command window here" title as the directory it was invoked from

3

1

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

laggingreflex

Posted 2014-11-15T07:19:44.217

Reputation: 3 498

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

Answers

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

Mario

Posted 2014-11-15T07:19:44.217

Reputation: 3 685

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") – laggingreflex – 2014-11-17T09:00:21.490

@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 – 2014-11-17T09:04:53.447

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". – laggingreflex – 2014-11-17T09:07:53.213

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 – 2014-11-17T09:15:58.937

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: http://superuser.com/questions/414155/cmd-exe-customizing-the-open-command-prompt-here-shell-extension (Earlier when writing my answer to your question I forgot that the question was already answered somwhere else)

– MBu – 2014-11-17T09:19:10.333

@MBu That will result in the whole path being in your caption bar. The goal has been to have only the actual folder name (but not the path). – Mario – 2014-11-17T09:20:09.247

You are right, sorry :-) – MBu – 2014-11-17T09:21:24.870

@MBu It's closely related, just a bit expanded. Also the special folders are a good note I completely forgot about. :) – Mario – 2014-11-17T09:24:10.193

Rewrite this for loop to ignore spaces and dots in a folder path: should be @for /F "tokens=*" %%G in ("%CD%") do @title %%~nG%%~xG and I'd use cd /D instead pushd as I do not need appropriate popd probably – JosefZ – 2014-11-17T11:30:19.687

@JosefZ Interesting. :) I've kept the pushd simply due to the fact that it's the default value. – Mario – 2014-11-17T11:48:12.820

And found there should be doubled percentage sign %% in the code I have provided: in ("%CD%"). However, here are available valid syntaxes for those registry values: either cmd.exe /D /K cd /D "%1" && @for /F "tokens=*" %%G in ("%1") do @title %%~nG%%~xG or cmd.exe /D /K cd /D "%1" && @for /F "tokens=*" %%G in ("%%CD%%") do @title %%~nG%%~xG. Notice doubled ampersand sign, please: renaming does not perform if change directory fails for some reason, thus user is immediately informed: something was wrong – JosefZ – 2014-11-17T12:09:37.193

@JosefZ I tested it out and it seems it has to be %V, %1 or %CD% don't work. cmd.exe /D /K cd /D "%V" && for /F "tokens=*" %%G in ("%V") do @title %%~nG%%~xG. Thanks for making the directories with spaces work. – laggingreflex – 2014-11-17T13:12:58.263

Towards "%V" versus "%1": IMHO, as I am on retired Win-XP, it's an instantiated event of Microsoft's approach to backward compatibility :-) – JosefZ – 2014-11-17T14:44:11.383

I've also found that cd instead of pushd makes the autorun.cmd (as mentioned in my orig ques) doesn't work. It seems pushd is better option. – laggingreflex – 2014-12-02T18:11:50.043

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.

JosefZ

Posted 2014-11-15T07:19:44.217

Reputation: 9 121

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

mike_n

Posted 2014-11-15T07:19:44.217

Reputation: 1

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 – 2018-10-09T22:42:27.127

Also, the linked post was deleted, so this link points nowhere. – fixer1234 – 2018-10-10T06:50:56.303