Mega-ZBlog

You are here.

Mega-ZBlog header image

Display the Path in the Command Prompt Title Bar

August 27th, 2008 · 4 Comments · Windows

Well I messed around a bit and came up with a rather nice solution. This emulates a feature of Linux’s gnome-terminal where you can set the titlebar to always reflect the terminal’s current directory.

Scroll down for code and instructions, or read on for “the process”.

I used this as a basis, but there are a number of things wrong with that article. First of all, the %=c:% variable returns the current directory on C:, not on the current drive. Since everyone except maybe some ultralight laptop owners have more than just a C: drive (I spend most of my time on E:, F:, and G: myself) this is unacceptable to me.

I quick search turned up %cd% which returns the current directory. Much nicer.

Now we have another little problem in the form of an extra prompt line that appears. I discovered by inserting “echo off” (which suppresses the prompt) before the first command in the doskey macro, then “echo on” at the end, this problem goes away quite nicely. I was almost done.

Then I just have to duplicate this doskey macro for other commands the article fails to consider, for example pushd and popd, plus all the drive letters. A for loop takes care of the latter, although I still can’t avoid the tedious typing of every letter in the alphabet (I need to start using powershell or cygwin bash or something).

In the end, here is what I have. Place both files in your system path somewhere (C:\WINDOWS\system32 meets this requirement).

titlepath.cmd

@echo off
doskey cd=echo off$Tcd $*$Ttitlepathhelper$Techo on
doskey chdir=echo off$Tchdir $*$Ttitlepathhelper$Techo on
doskey pushd=echo off$Tpushd $*$Ttitlepathhelper$Techo on
doskey popd=echo off$Tpushd $*$Ttitlepathhelper$Techo on
for %%x in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do doskey %%x:=echo off$T%%x: $*$Ttitlepathhelper$Techo on
titlepathhelper

titlepathhelper.cmd

@title %cd%

You’ll notice titlepath takes a little bit to churn away… if it bothers you, you can remove some of the drive letters from the for list or remove that line completely if you don’t mind the title not updating when you change drives.

One last thing is if you want this to happen every time you launch a command prompt without having to type titlepath in.

Open HKEY_CURRENT_USER\Software\Microsoft\Command Processor in Registry Editor and make an AutoRun string value and set it to titlepath.cmd. Interestingly my batch file shouldn’t work at all according to this caveat, but I can’t get it to AutoRun when running through a for loop… odd.

Tags: ···

4 responses so far ↓

  • 1 Olivier Dagenais // Aug 27, 2011 at 1:56 pm

    Awesome, thank you for sharing! My console window titles are no longer this mysterious, opaque thing that I have to click on to find out which window is which, while secretly cursing the lack of a more *NIXish bash 🙂

    Cheers,
    – Oli

  • 2 Johnny Baloney // Dec 9, 2011 at 10:39 am

    Nice! Thanks 😀

  • 3 Donnie // Sep 12, 2013 at 8:35 pm

    Thanks so much for this post!

  • 4 Jayce // Oct 1, 2013 at 11:41 am

    Awesome!!! Even though it doesn’t auto update, I usually stay in the same folder all the time anyways. The only reason I do change is because I can’t find the prompt i’m looking for! Now I can!

    Thanks!

You must log in to post a comment.