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

3 Answers3

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
  • 4,344
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
  • 13,855
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
  • 1