Setting win32 console title from Python

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 28 11:37:57 EDT 2005


runes wrote:

> Hi Jay. It seems like my requirement is a light edition of your. I like
> having many console windows open, and to make it easier to switch
> between them, I like to name them.  Todays solution is rather tedious
> 
> - a batch file that calls a python script that isolates the directory
> name and stores it in temp file the batch file reads and use as
> argument in the title command.
> 
> It works fine, but I dislike the combination and the entire concept of
> having to create a temporary file for such a small task. The "batch
> language" is probably the most terrible scripting environment ever
> created ;-)

As I showed in my other post you can parse program output without using a 
temporary file.
 
If all you want to do is to run a script which sets the title when CMD.exe 
starts, that is actually quite easy:

---- c:\temp\startcmd.py ----
import os
print "Python startup"
os.execv('c:\\windows\\system32\\cmd.exe',
     ["/D", "/C", "title", "CMD - " + os.getcwd()]
-----------------------------

Then run regedit and find the key 
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

edit it and insert the name of your script (in this example 
c:\temp\startcmd.py).

Now whenever you start a new command processor the script will set the 
title and CMD will NOT reset it. It seems that if you set the title from a 
subprocess when CMD is starting it will accept your change.

Warning: don't use os.system from the startcmd.py script as that would run 
CMD.exe without the /D flag which would run the script recursively as it 
starts.



More information about the Python-list mailing list