How to set Title of CMD to current working Directory in Windows XP

19

9

How does one set the title of the of the Command Prompt (CMD) in Windows XP to the current working directory dynamically ? I can use

title %CD%

however, this is a temporary fix and the title remains fixed when I change directory using the CD command.

Stormshadow

Posted 2010-05-20T13:01:56.210

Reputation: 747

Well, what happens when you use pushd with one of the macros? – Hello71 – 2010-07-05T21:27:48.480

Answers

21

Got it to work thanks to gravvity's doskey macro. He has used && to combine the cd and title commands which works perfectly. I even made this macro load every time I use cmd by tweaking the registry.

1) I created a bat file called cmd_title.bat and it contents are

@echo off
title %cd%

2) I placed this file in the C: drive (C:\cmd_title.bat)

3) Create another batch file called cmd.bat in the C: drive with the following contents

doskey cd = cd /d $* ^&^& "C:\cmd_title.bat"
title %cd%

(the /d flag is for using cd to switch to another drive).

4) Then we open regedit and go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor. Here there is a key called AutoRun. We modify the value of this key and set it to the location of the cmd.bat file in quotes (eg: "C:\cmd.bat").

Now cd works as we want every time we open cmd.

Basically && is used for command chaining in Windows

Stormshadow

Posted 2010-05-20T13:01:56.210

Reputation: 747

@bits There is a solution to make it work with shift+right click here: https://superuser.com/questions/414155/cmd-exe-customizing-the-open-command-prompt-here-shell-extension

– kapex – 2014-10-15T22:56:52.987

If you want it to run it on particular command prompt windows, create a new shortcut and append "/k c:\cmd.bat" to the shortcut's target – Kevin Shea – 2015-08-14T11:05:18.867

2Nice solution! You could also do it without the cmd_title.bat if you type the macro like this instead: doskey cd=@echo off$Tcd /d $*$T@title ^%cd^%$Techo on – Superole – 2015-12-21T17:24:40.630

2I find @echo off is not necessary if you just add @ in front of the commands you don't want echoed. I also found this command more useful to set the title to just the current directory name instead of the full path @for %%* in (.) do @title %%~nx* – Bron Davies – 2016-09-14T19:21:56.127

1This solution works, but it means that you cannot add your own /d flag when you call cd. For instance:"H:>cd /d V:" gives you the message "The syntax of the command is incorrect." I actually think that cd should always work like cd /d, and this will save me some typing, so I consider it an improvement. However, it's worth mentioning. – Alan – 2017-07-20T14:01:56.443

1Also note that with this system, if you simply type a drive letter (e.g., "V:") to go to a directory, the title won't get updated. You need to use "cd" (e.g., "cd V:"). – Alan – 2017-07-20T14:20:22.257

Cool, I got your solution to work. But I lost my "hit tab key, auto-match" functionality. – rok2791SWTS – 2010-09-09T17:39:10.377

1You might have disturbed the CompletionChar/PathCompletionChar values in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor key in the registry. To set use the tab key for auto-completion you need to set the CompletionChar and PathCompletionChar values to 0x9 (hex) – Stormshadow – 2010-09-10T05:44:49.930

1+1 it is a working solution, but with minor correction: AutoRun is not a key, but value of type of string. – mloskot – 2011-12-06T23:53:53.873

When we shift+right click on a folder in windows explorer, we have an option for opening command prompt on that folder. This solution doesn't work in that case. Does anyone know a quick fix to this? – bits – 2013-02-26T19:13:17.700

6

I think that pushd and popd are much more useful than cd, and would see a lot more use if they were quicker to type. I have resolved the issues of cd vs. pushd/popd and console window directory title with the following script, which I call d.bat, which is in my path.

@ echo off
rem d.bat replaces CD, PUSHD, and POPD with one command that also changes the title
rem of the console window to tell the current directory. Invoked with no arg, the
rem title is updated. Use this after changing the directory by some other means.
rem The argument / invokes popd. Any other argument invokes pushd with that arg.

if not _%1 == _ ( 
    if _%1 == _/ (
        popd
    ) else (
        pushd %*
    )
)
title %CD%

David McCracken

Posted 2010-05-20T13:01:56.210

Reputation: 61

2

You can change the Command Prompt's title by using the title command.

You may create a batch file (say mycd.bat) containing:

title "%1"
cd  "%1"

and use it instead of "cd" :

mycd "newdir"

You can also put the .bat file in system32 if you wish it to always be available.

harrymc

Posted 2010-05-20T13:01:56.210

Reputation: 306 093

5Using macros would be a better option: doskey cd=cd $* ^&^& title $* – user1686 – 2010-05-20T13:17:25.260

@grawity: Good idea. – harrymc – 2010-05-20T14:53:14.283

1

Create a file: cd.bat and put the following in there:

cd %*  
title %CD%  

Then issue this command:

doskey cd=...\cd.bat $*  

Replace "..." with the path to cd.bat. Try it out and see that it works.

To make that permanent-ish, create dosrc.cmd, put that doskey command in there, and then create a cmd shortcut, right click on it and select properties.

Modify Target to be:

%windir%\system32\cmd.exe /K ...\dosrc.cmd  

Again, replace "..." with the path to dosrc.cmd

Then double click on the shortcut, you should have the cd functionality.

Mauro

Posted 2010-05-20T13:01:56.210

Reputation: 11

1

You can't, at least not with the Windows shell.


It might be possible to add "set Xterm title" escape sequences to %PROMPT%, but you would need a different terminal emulator (perhaps PuTTYcyg or something from SfU), as Windows Console does not support escape sequences.

Alternatively, find another shell which can use the Windows console functions to set titles.


These are often confused in Windows contexts, so...

shell reads and interprets input; cmd.exe, command.com, /bin/sh

terminal, terminal emulator, console displays text-based programs (including the shell) on your screen; Windows Console, xterm, PuTTYcyg

user1686

Posted 2010-05-20T13:01:56.210

Reputation: 283 655

actually, cmd.exe is a terminal. explorer is a shell. – Rook – 2010-05-20T13:09:06.273

2@Idigas: explorer is a GUI shell, cmd.exe is a text-based one. (Think about it: cmd.exe is the exact equivalent of /bin/sh in Unix, and /bin/sh is always called "the shell". Similarly, the Windows Console is equivalent to a "terminal emulator" in X11.) – user1686 – 2010-05-20T13:14:15.730

I stand corrected. – Rook – 2010-05-20T13:54:05.167

1

Assuming \tools is in the path, and it's where you keep your batch files:

copy con: \tools\titlecmd.txt

title ^Z

copy con: \tools\cdtitle.bat

cd>\tools\cd.txt

copy \tools\titlecmd.txt+\tools\cd.txt cdtitletmp.bat

call cdtitletmp

^Z

You now have a cdtitle.bat that you can call from another batch file.

daveand

Posted 2010-05-20T13:01:56.210

Reputation: 11