Back

Setting the title of console window to current directory

Added: August 21, 2009

Tags: hint script

I had some problems to search for all necessary details so I am going to publish my little script here as it might be useful for somebody.

In my work I need to keep several console windows open (on Windows) in order to produce jar artefacts from several dependent projects without a need to use our build server. Sometimes it got a bit out of control when I needed 6 or 8 of them opened at the same time and was not easy to find partcular one quickly. So I have decided to create a script that changes console title to working directory (only last directory).

ti.bat:

@echo off
setlocal

set name=%cd%

set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%
set name=%name:*\=%

title=%name%

endlocal
Explanation of non-obvious things follows:
  • set name=%cd% gets name of working directory.
  • following collection of lines is my workaround for a loop that did not work for me well. If you can help me to get rid of this duplication (and limit to 10 subdirectories) I will be very glad.
  • set name=%name:*\=% removes text from variable up to the first backslash or it does nothing if there is no backlash.
  • setlocal, endlocal keep changes in variables private for script execution, changes are forgotten when it ends.
So when I open a new console to perform my building tasks I can write ti [ENTER] and the title is nicely renamed from original not very useful C:\WINDOWS\system32\cmd.exe to something like dao_testing.

Back