how do I set the window title to the current directory path?

35 views
Skip to first unread message

Dallas

unread,
Apr 14, 2022, 11:44:41 AMApr 14
to
In a cmd.exe shell session how do I set the window title to the current directory path?

John Gray

unread,
Apr 14, 2022, 12:18:19 PMApr 14
to
On Thursday, 14 April 2022 at 16:44:41 UTC+1, Dallas wrote:
> In a cmd.exe shell session how do I set the window title to the current directory path?

title %cd%

Dallas

unread,
Apr 14, 2022, 12:26:06 PMApr 14
to
Thanks! Earlier I tried looking at the output of the SET command, but it turns out that %CD% is
not listed by SET.

Now I want to know what else the SET command does not disclose.


Dallas

unread,
Apr 14, 2022, 12:44:38 PMApr 14
to
On 4/14/2022 11:18 AM, John Gray wrote:
Now, how do I get rid of that "Administrator: " prefix in the window title and still run as
administrator?


John Gray

unread,
Apr 14, 2022, 1:16:06 PMApr 14
to
For assorted built-in environment variables, see https://ss64.com/nt/syntax-variables.html
I don't think it is possible to remove the "Administrator: " prefix from the window title; someone may know a way.

Dallas

unread,
Apr 14, 2022, 3:21:17 PMApr 14
to
On 4/14/2022 12:16 PM, John Gray wrote:
> I don't think it is possible to remove the "Administrator: " prefix from the window title; someone may know a way.

I suppose now I need to re-think why I like to run as administrator.


JJ

unread,
Apr 15, 2022, 6:59:25 AMApr 15
to
They're mostly listed in the end of SET's help. Type: set /?

To display undocumented internal variables, type: set;
The variable names starts with: =

Dallas

unread,
Apr 15, 2022, 8:44:08 AMApr 15
to
Thanks!

set /? | findstr "^%"

set; | findstr "^="

Ammammata

unread,
May 2, 2022, 9:26:23 AMMay 2
to
Il giorno Thu 14 Apr 2022 06:18:18p, *John Gray* ha inviato su
alt.msdos.batch.nt il messaggio
news:ed9b3dae-1ff0-4ff4...@googlegroups.com. Vediamo
cosa ha scritto:

>> In a cmd.exe shell session how do I set the window title to the
>> current directory path?
>
> title %cd%
>

it works, but when I change folder it does not update


--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........

Kenny McCormack

unread,
May 2, 2022, 10:03:18 PMMay 2
to
In article <XnsAE8B9D0FB62EFam...@127.0.0.1>,
Ammammata <amma...@tiscalinet.it> wrote:
>Il giorno Thu 14 Apr 2022 06:18:18p, *John Gray* ha inviato su
>alt.msdos.batch.nt il messaggio
>news:ed9b3dae-1ff0-4ff4...@googlegroups.com. Vediamo
>cosa ha scritto:
>
>>> In a cmd.exe shell session how do I set the window title to the
>>> current directory path?
>>
>> title %cd%
>>
>
>it works, but when I change folder it does not update

One way or the other, you will need to alias your "cd" command, so that
whenever you change directories, it runs the above "title" command for you.

I tried to do it with "doskey", like this:

doskey cd=cd $* ^& title ^%cd^%

and it almost worked, except that %cd% gets expanded before the "cd"
command executes, so it is out of date. It seems like the delayedexpansion
thing would fix this, but I found out that delayedexpansion only works in
batch files; it doesn't work at the prompt. And, of course, doskey macros
are command line things, not really batch files. It would be nice if there
was a way of making DOSKEY aliases that span multiple lines, instead of
having to slog everything together with &s.

I think it could be done with an actual batch file, but I lost interest in
pursuing it any further...

--
They say that Trump is a stupid man's idea of a clever man, a poor man's
idea of a rich man, and a weak man's idea of a strong man.

Well, Melania is an unclassy man's idea of classy.

Herbert Kleebauer

unread,
May 3, 2022, 10:49:28 AMMay 3
to
On 03.05.2022 04:03, Kenny McCormack wrote:

>>>> In a cmd.exe shell session how do I set the window title to the
>>>> current directory path?
>>>
>>> title %cd%
>>>
>>
>>it works, but when I change folder it does not update
>
> One way or the other, you will need to alias your "cd" command, so that
> whenever you change directories, it runs the above "title" command for you.
>
> I tried to do it with "doskey", like this:
>
> doskey cd=cd $* ^& title ^%cd^%
>
> and it almost worked, except that %cd% gets expanded before the "cd"
> command executes, so it is out of date.

I don't know anything about doskey so I just randomly
inserted % and ^ in your doskey command and this works
here (but don't understand why).

set dd=cd
doskey cd=cd $* ^&call title %^%dd^%%

Kenny McCormack

unread,
May 3, 2022, 11:30:18 AMMay 3
to
In article <t4rfdk$p24$1...@gioia.aioe.org>,
Herbert Kleebauer <kl...@unibwm.de> wrote:
...
>> I tried to do it with "doskey", like this:
>>
>> doskey cd=cd $* ^& title ^%cd^%
>>
>> and it almost worked, except that %cd% gets expanded before the "cd"
>> command executes, so it is out of date.
>
>I don't know anything about doskey so I just randomly
>inserted % and ^ in your doskey command and this works
>here (but don't understand why).
>
>set dd=cd
>doskey cd=cd $* ^&call title %^%dd^%%
>

Indeed it does! Kudos to you for figuring that out.

I suppose this is one of those things where figuring out how to do it is
the first step and then figuring out why it works is the next step.

I wonder if it has something to do with the fact that %cd% isn't really a
normal variable; it is what you might call a "pseudo-variable". Somehow,
that causes it to be evaluated differently. And, somehow, when you put in
"dd" in place of "cd", it causes it to go back to the regular evaluation rules.

Anyway, as far as DOSKEY goes, all I know, I got from doing: doskey /?
It is pretty murky stuff. The fact that the parameters are referenced with
a $ sign (like in Unix), rather than with % (like in regular batch files)
always trips me up.

Final question: Just out of curiosity, why do you omit the usual space
after the & in your rendition of the doskey command? I know it doesn't
matter, but it seem "ugly" that way. I've noticed that you've done this in
the past as well (in prior postings to this ng).

--
I've learned that people will forget what you said, people will forget
what you did, but people will never forget how you made them feel.

- Maya Angelou -

Herbert Kleebauer

unread,
May 3, 2022, 12:43:26 PMMay 3
to
On 03.05.2022 17:30, Kenny McCormack wrote:

>>set dd=cd
>>doskey cd=cd $* ^&call title %^%dd^%%
>>

> Final question: Just out of curiosity, why do you omit the usual space
> after the & in your rendition of the doskey command? I know it doesn't
> matter, but it seem "ugly" that way. I've noticed that you've done this in
> the past as well (in prior postings to this ng).

After the & starts a new command and because I don't insert a
space before a command at the CMD prompt I also don't insert
a space before the command after an &.

But more important is the space before the &. You can write
the two commands above in one line:

set dd=cd&doskey cd=cd $* ^&call title %^%dd^%%

But if you insert a space before the & it doesn't work
anymore:

set dd=cd &doskey cd=cd $* ^&call title %^%dd^%%

Kenny McCormack

unread,
May 3, 2022, 2:15:47 PMMay 3
to
In article <t4rm3a$6dq$1...@gioia.aioe.org>,
Yup. All true.

Ain't DOS batch a fine programming langauge???

--
If you don't have faith, it's because you are reading the Bible with an
honest, truthful, real-answer seeking heart.

- Rick C Hodgin -

R.Wieser

unread,
Jul 3, 2022, 4:13:11 PMJul 3
to
crossposted to crossposted to microsoft.public.windowsxp.general

Kenny,

For some "odd reason" I had the same question but posted it into the
microsoft.public.windowsxp.general. Herbert Kleebauer alerted me to this
thread.

>>set dd=cd
>>doskey cd=cd $* ^&call title %^%dd^%%
>>
>
> Indeed it does! Kudos to you for figuring that out.

Herbert also posted your final(?) solution :

> set dd=cd&doskey cd=cd $* ^&call title %^%dd^%%

I think that you have found out by now that that doesn't quite work. For
multiple reasons :

Take a look at what "doskey /macros" shows you. You might notice that the
stored command replacement is this : "cd=cd $* &call title dd%". Which
ofcourse means that the title of your command window will always be "dd%"

Easy to fix, either use "^%%dd^%%" - or just "%%dd%%". ("^%dd^%" doesn't
work)

The biggest problem however is that the "set dd=cd&" part of your command
isn't stored in the doskey command replacement. Which ofcourse means that
the current directory is ony *once* stored into the "dd" variable, and than
used for all the window captions. And I don't think that is what you
want(ed). :-)

Also, IFAIKS that "call" in there is not needed.


With the help of JJ I found a solution which seems to work nicely :

doskey cd=cd /d $* ^& for %%A in (.) do @title %%~nxA

(caveat: I did my work under XPsp3)

Regards,
Rudy Wieser


Herbert Kleebauer

unread,
Jul 3, 2022, 5:19:09 PMJul 3
to
On 03.07.2022 22:13, R.Wieser wrote:
> crossposted to crossposted to microsoft.public.windowsxp.general
>
> Kenny,
>
> For some "odd reason" I had the same question but posted it into the
> microsoft.public.windowsxp.general. Herbert Kleebauer alerted me to this
> thread.
>
>>>set dd=cd
>>>doskey cd=cd $* ^&call title %^%dd^%%
>>>
>>
>> Indeed it does! Kudos to you for figuring that out.
>
> Herbert also posted your final(?) solution :
>
>> set dd=cd&doskey cd=cd $* ^&call title %^%dd^%%
>
> I think that you have found out by now that that doesn't quite work. For
> multiple reasons :

Did you try it? Here it works.

> Take a look at what "doskey /macros" shows you. You might notice that the
> stored command replacement is this : "cd=cd $* &call title dd%". Which
> ofcourse means that the title of your command window will always be "dd%"

Here I get:

D:\>set dd=cd&doskey cd=cd $* ^&call title %^%dd^%%

D:\>doskey /macros
cd=cd $* &call title %%dd%%


> Easy to fix, either use "^%%dd^%%" - or just "%%dd%%". ("^%dd^%" doesn't
> work)

The fix is the bug!

> The biggest problem however is that the "set dd=cd&" part of your command
> isn't stored in the doskey command replacement. Which ofcourse means that

It don't have to be stored in the doskey macro. It is just a one line
replacement for:

set dd=cd
doskey cd=cd $* ^&call title %^%dd^%%


> the current directory is ony *once* stored into the "dd" variable, and than
> used for all the window captions. And I don't think that is what you
> want(ed). :-)

Not the current directory is stored in the variable "dd" but the
string "cd"


> Also, IFAIKS that "call" in there is not needed.

Without the call it wouldn't work.

R.Wieser

unread,
Jul 4, 2022, 3:53:32 AMJul 4
to
Herbert,

>> I think that you have found out by now that that doesn't quite work. For
>> multiple reasons :
>
> Did you try it? Here it works.

When I doubt something works I normally test it before posting about it.
As I've done this time too.

>> Easy to fix, either use "^%%dd^%%" - or just "%%dd%%". ("^%dd^%" doesn't
>> work)
>
> The fix is the bug!

You might want to explain that.

As mentioned, when I use "doskey /macros" I see that that "%^%dd^%%" has
been turned into "dd%" - which definitily doesn't give the sought-for
result.

>> The biggest problem however is that the "set dd=cd&" part of your command
>> isn't stored in the doskey command replacement. Which ofcourse means
>> that
>
> It don't have to be stored in the doskey macro. It is just a one line
> replacement for:

Yeah, you already said that. The problem is that you are not *explaining*,
nor do I see you give any test results.

> Not the current directory is stored in the variable "dd" but the string
> "cd"

Oh blimy, I overlooked that the "cd" there doesn't have "%" signs around it
(set dd=%cd%"). That makes it even worse : What you end up with is "title
cd". And at least here that "cd" there is regarded as *text*, not as a
command.

(ignore this. I now understand how your posted solution works)

>> Also, IFAIKS that "call" in there is not needed.
>
> Without the call it wouldn't work.

It does here.

Maybe you should tell us which OS you're using / that "alt.msdos.batch.nt"
newsgroup is ment for ?

AFAIKS you seem to think that the solution you posted (you got from there)
should also work under XP. And as I've been telling you, it doesn't.

>> As a check I put the above in a batchfile,
>
> You shouldn't have done that. Just enter it at the command prompt.

Ackkk.... thats quite a bit of a difference. I've just tried it that way
and it works. And now I also understand why you need that "call" in
there.

Not usefull to me though, as I have zero wish to type it in every time I
open a command window. :-(

And I would still suggest to put that "set dd=cd" part *inside* the doskey
macro - other batchfiles could overwrite the "dd" environment variable and
leave you with unexpected results.

Regards,
Rudy Wieser

P..s
I can't seem to figure out how to put more "%" / "^%" sequences around that
"%^%dd^%%" to get it to work when started from a batchfile. (yeah, I already
have a working solution. That doesn't mean I'm not interrested in other
solutions too).


Herbert Kleebauer

unread,
Jul 4, 2022, 6:52:52 AMJul 4
to
On 04.07.2022 09:53, R.Wieser wrote:

>>> As a check I put the above in a batchfile,
>>
>> You shouldn't have done that. Just enter it at the command prompt.
>
> Ackkk.... thats quite a bit of a difference. I've just tried it that way
> and it works. And now I also understand why you need that "call" in
> there.
>
> Not usefull to me though, as I have zero wish to type it in every time I
> open a command window. :-(

It is the same as like

for %i in (hello world) do echo %i

It does work on the command line but not in a batch file. If you want
to use it in a batch file, you have to add a few %:

set dd=cd
doskey cd=cd $* ^&call title %%%%dd%%%%


> And I would still suggest to put that "set dd=cd" part *inside* the doskey
> macro - other batchfiles could overwrite the "dd" environment variable and
> leave you with unexpected results.

You can use any name which is not used by other batch code instead
of "dd". But if there is a naming conflict with an other batch, then
maybe it is better the doskey macro stops working than the other
batch stops working.

R.Wieser

unread,
Jul 4, 2022, 7:46:23 AMJul 4
to
Herbert,

> It does work on the command line but not in a batch file. If you want
> to use it in a batch file, you have to add a few %:

I know. I just didn't get it to work.

> set dd=cd
> doskey cd=cd $* ^&call title %%%%dd%%%%

And as is appears now I was just too focussed on keeping those "^" in there.
You /said/ they where part of the solution. :-)

> You can use any name which is not used by other batch code instead of
> "dd".

I've got another solution - not using an environment variable :

You could use my origional solution (from before you posted, with the "for
%%A" in it) and remove the "~nx" part. The below also works :

(from within a batchfile)

doskey cd=echo off^&cd $*$Ttitle %%cd%%^&echo on

The trick is that the "$T" in there takes the place of the "call", but also
delays the expansion of the %CD% variable. The "echo off" and "echo on"
parts are needed because "$T" displays another prompt.

Thanks for your responses. I had some fun trying the different
possibilities out.

Regards,
Rudy Wieser

P.s.
I still like my solution which only displays the last folders name best
though. :-)


Kenny McCormack

unread,
Jul 4, 2022, 8:08:14 AMJul 4
to
In article <t9st8l$1pl8$1...@gioia.aioe.org>,
R.Wieser <add...@not.available> wrote:
>crossposted to crossposted to microsoft.public.windowsxp.general
>
>Kenny,
>
>For some "odd reason" I had the same question but posted it into the
>microsoft.public.windowsxp.general. Herbert Kleebauer alerted me to this
>thread.

Keep in mind that most of what you attribute to me (as in, "Your (Kenny's)
code, blah, blah, blah") is not mine at all. Most of it was dreamed up,
composed, and posted by Herbert. So, credit where credit is due.

In particular, if you see code that omits the space after the &, that's a
Herbert trademark.

N.B. I'm not saying it is wrong. In fact, it is quite right to do that.
But most code that I wrote won't have it.

Just tryin' to help you do your archaeology...

--
There are many self-professed Christians who seem to think that because
they believe in Jesus' sacrifice they can reject Jesus' teachings about
how we should treat others. In this country, they show that they reject
Jesus' teachings by voting for Republicans.

R.Wieser

unread,
Jul 4, 2022, 8:22:42 AMJul 4
to
Kenny,

> Keep in mind that most of what you attribute to me (as in, "Your (Kenny's)
> code, blah, blah, blah") is not mine at all. Most of it was dreamed up,
> composed, and posted by Herbert. So, credit where credit is due.

My apologies to both of you. I thought it was you who put it together with
Herbert commenting upon it.

Regards,
Rudy Wieser


R.Wieser

unread,
Jul 4, 2022, 8:22:42 AMJul 4
to
Herbert,

I wrote :

>> And as is appears now I was just too focussed on keeping those "^" in
>> there. You /said/ they where part of the solution. :-)

I got a brainfart and thought of something else to try :

set dd=cd&doskey cd=cd $* ^&call title %%^dd%%

(see the environment variable at the end)

It also works, and is easier to read & convert to in-batchfile usage.

Regards,
Rudy Wieser


Kenny McCormack

unread,
Jul 4, 2022, 9:40:49 AMJul 4
to
In article <t9um2g$1vjc$1...@gioia.aioe.org>,
Actually, some other poster originally asked the question, then I suggested
a partial solution that almost works, but I conceded that it did not work 100%
and I didn't care to spend any more time on it. Herbert then picked it up
and ran with it from there.

--
There are a lot of Wisconsin farmers right now who, despite having
themselves voted for Trump, are now wishing that their state's electors
had had the good sense to vote for the other candidate - thereby saving
them from their current predicament.

R.Wieser

unread,
Jul 4, 2022, 9:43:19 AMJul 4
to
Herbert,

I wrote :

> I got a brainfart and thought of something else to try :

Got another one : no intermediate environment variable and, IMHO, even
easier to work with :

set doskey cd=cd $* ^&call title %%c^^d%%

(from within a batchfile)

... I must have /way/ to much time on my hands.

Regards,
Rudy Wieser


Kenny McCormack

unread,
Jul 4, 2022, 9:48:57 AMJul 4
to
In article <t9uqpm$5eb$1...@gioia.aioe.org>,
Sounds like it. Sounds like you lead a happy life.
Congrats.

--
He must be a Muslim. He's got three wives and he doesn't drink.

Herbert Kleebauer

unread,
Jul 4, 2022, 11:50:31 AMJul 4
to
On 04.07.2022 15:43, R.Wieser wrote:

> Got another one : no intermediate environment variable and, IMHO, even
> easier to work with :
>
> set doskey cd=cd $* ^&call title %%c^^d%%
>
> (from within a batchfile)

That is a good one. For me batch programming is "trial and error"
and I would have never found this solution.

R.Wieser

unread,
Jul 4, 2022, 2:01:58 PMJul 4
to
Herbert,

> That is a good one. For me batch programming is "trial and error" and I
> would have never found this solution.

Although I know know a few things about batch programming, new areas are
mostly "trial and error" to me, just like for you.

It just struck me that if a "^" (escape char) could be used to tinker around
with the "%" and "&" marks like you did that there would be a possibility
that it would also work when applied to the environment variable. And as
the reason for the "dd" variable was to keep the "%cd%" variable from being
resolved too early it was a short step to putting enough "^" markers next to
the "cd" chars themselves to keep them from the "%cd% sequence being
recognised until the last step. Putting those "^" between the "c" and "d"
just made the whole string look balanced / nice.

Yep, a lot of "trial and error" I'm afraid. I've now got a batchfile with
over 10 attempts (the ones I cared to keep. Must have gone thru over 20 of
them - including ones with "echo" instead of "title") that all failed in
different ways. :-)

Regards,
Rudy Wieser


R.Wieser

unread,
Jul 5, 2022, 3:37:45 AMJul 5
to
Herbert,

> set doskey cd=cd $* ^&call title %%c^^d%%
>
> (from the within a batchfile)

This morning I woke up with a "I /have/ too look into that" thought : the
above can be interfered with.

Just type "set c^^d=foobar" on the commandline, and that "foobar" will be
all the caption you get.

IOW, trying to hide an evironment variables name isn't foolproof.

... even when I sleep I seem to have too much time on my hands. :-)

Regards,
Rudy Wieser


Zaidy036

unread,
Jul 5, 2022, 10:54:50 AMJul 5
to
On 4/14/2022 11:44 AM, Dallas wrote:
> In a cmd.exe shell session how do I set the window title to the current
> directory path?
Maybe this will help:
<https://serverfault.com/questions/255291/get-directory-containing-the-currently-executed-batch-script>



R.Wieser

unread,
Jul 8, 2022, 4:41:53 AMJul 8
to
Herbert,

I was reading back thru the thread and saw this :

> set dd=cd&doskey cd=cd $* ^&call title %^%dd^%%
>
> But if you insert a space before the & it doesn't work
> anymore:
>
> set dd=cd &doskey cd=cd $* ^&call title %^%dd^%%

Thats explainable : "set" takes *everything* from the "=" upto the end of
the line and puts it into the variable (when using an "&" that becomes "the
end of the line")

set foo=the quick brown fox
echo %foo%

now try this :
- - - - - - - - - - - -
set dd=cd&rem
echo [%dd%]
- - - - - - - - - - - -
(no space infront of the "&")

and than this
- - - - - - - - - - - -
set dd=cd &rem
echo [%dd%]
- - - - - - - - - - - -
(one-or-more spaces infront of the "&")

the "[" and "]" are just visual delimitors and have no special meaning.

You might notice that the second example displays a space infront of the
"]", while the first one doesn't.

you will get the same results when you leave the "&rem" parts off

Regards,
Rudy Wieser



Reply all
Reply to author
Forward
0 new messages