How to get rid of the extra character ahead of the title

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

How to get rid of the extra character ahead of the title

#1 Post by jfl » 27 Jan 2021 12:42

As search does not work anymore on the forum (Anybody looking at this?), I'm turning to other peoples' memory...

I noticed that the title command inserts an extra space in front of the title string.
By default, my cmd console has the title:

Code: Select all

Administrator: Command Prompt
But if I run:

Code: Select all

title Command Prompt
I clearly see my title jump to the right, like this:

Code: Select all

Administrator:  Command Prompt
As if the space following the title command were included in the title string!
Thinking about how to get rid of that space, I thought about trying the other token separators: , ; =
Surprise (for me): They do indeed their token separation task... but they too are displayed on the title! :shock: Ex:

Code: Select all

title=Command Prompt
displays:

Code: Select all

Administrator: =Command Prompt
This confirms the hypothesis that the extra space I was seeing is the token separator...
But I still wonder if there's a batchly-correct way to get rid of that extra space or token separator?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: How to get rid of the extra character ahead of the title

#2 Post by Squashman » 27 Jan 2021 14:37

It has to be an affect of running an elevated cmd prompt.

In a normal cmd prompt if you do

Code: Select all

title=blah
The equals symbol does not end up in the Window Title.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to get rid of the extra character ahead of the title

#3 Post by penpen » 27 Jan 2021 14:52

You could use the opening bracket:

Code: Select all

title(blah
But if it adds an extra space, then the initial title also had it (at least on my windows 10).


penpen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to get rid of the extra character ahead of the title

#4 Post by dbenham » 27 Jan 2021 15:20

The ( is included in the resulting title if you use title(blah

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to get rid of the extra character ahead of the title

#5 Post by penpen » 27 Jan 2021 16:49

Yes, my post was intended as a reaction to what Sqashman wrote.
Sorry, for confusing everyone.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: How to get rid of the extra character ahead of the title

#6 Post by Compo » 29 Jan 2021 10:21

I would assume that it's a trailing space suffixed to the pre-string, in the case, Administrator:[space], as opposed to, a leading space being prefixed to the title itself, i.e. [space]Title.

I have just tried inserting one or more backspace characters, to see if they would remove it, but they just show as prefiexed and unidentifiable box characters.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get rid of the extra character ahead of the title

#7 Post by aGerman » 29 Jan 2021 10:41

I tried to change the title using a wrapped SetConsoleTitle in a PowerShell one-liner. An ANSI escape sequence like
echo %ESC%]0;foo%BEL% (ESC 0x1B, BEL 0x07)
failed as well. The CMD alway believes to know it better :D

Steffen

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get rid of the extra character ahead of the title

#8 Post by aGerman » 04 Feb 2021 16:05

Proof of concept (Win 10)...

Code: Select all

@for /f "tokens=1*" %%a in ('forfiles /p "%~dps0." /m "%~nxs0" /C "cmd /d /c echo(0x1B 0x07"') do @set admintitle=start /b cmd /d /c "<nul set /p "=%%a]0;%%title%%%%b""
Save this line in a batch file (foo.bat in my screenshot below) and call it from within an elevated cmd window.
Assign a string to variable title.
Screenshot 1.jpg
Screenshot 1.jpg (44.08 KiB) Viewed 8222 times

Execute the %admintitle% macro.
Screenshot 2.jpg
Screenshot 2.jpg (47.49 KiB) Viewed 8222 times

start /b cmd /c seems to somehow bypass the restoration of the original title. Obviously this will change the whole title. However, if you like to see the preceded "Administrator:" then just specify it ... :wink:

Steffen

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: How to get rid of the extra character ahead of the title

#9 Post by jfl » 07 Feb 2021 13:08

Excellent, thanks! :D

I did not know that such Xterm escape sequences worked in the old console too.
This seems something new in Windows 10 though, because a quick test in Windows 7 did not work there.

Just curious, why do you set that admintitle variable, and invoke it as a second command afterwards?
This works just as well when the command is invoked within the script! For example, this script:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set TITLE=%*
for /f "tokens=1*" %%a in ('forfiles /p "%~dps0." /m "%~nxs0" /C "cmd /d /c echo(0x1B 0x07"') do start /b cmd /d /c ^"<nul set /p "=%%a]0;%%title%%%%b"^"
allows setting the title passed in its argument line, all in a single operation.
aGerman wrote:
04 Feb 2021 16:05
start /b cmd /c seems to somehow bypass the restoration of the original title.
Nice find.
This reminds me of a similar title restoration issue when a second batch is executed from a first one this way:
a.bat:

Code: Select all

@echo off
setlocal
echo This is a.bat
endlocal & b.bat
b.bat:

Code: Select all

@echo This is b.bat
Every time a.bat is invoked, the title line grows longer.

If you change the last line in a.bat to:

Code: Select all

endlocal & call b.bat
or to:

Code: Select all

endlocal
b.bat
the title is restored correctly.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get rid of the extra character ahead of the title

#10 Post by aGerman » 07 Feb 2021 14:20

jfl wrote:
07 Feb 2021 13:08
This seems something new in Windows 10 though, because a quick test in Windows 7 did not work there.
Yes, VT sequences came again with the new console on Win 10. If you need something more downward compatible, try wrapping SetWindowText (SetConsoleTitle didn't work in my tests).

Code: Select all

@set admintitle=start /b cmd /d /c powershell -nop -ep Bypass -c ^"$c=Add-Type -Name pInv -PassThru -MemberDefinition '^
[DllImport(\"user32.dll\",CharSet=CharSet.Auto)] public static extern int SetWindowTextW(IntPtr hWnd,string title);^
[DllImport(\"kernel32.dll\")] public static extern IntPtr GetConsoleWindow();';^
$null=$c::SetWindowTextW($c::GetConsoleWindow(),'%%title:'=''%%');^"
jfl wrote:
07 Feb 2021 13:08
Just curious, why do you set that admintitle variable, and invoke it as a second command afterwards?
For no particular reason. Eventually I could have made it a macro which accepts arguments. However, it was just a proof of concept in the first place.

jfl wrote:
07 Feb 2021 13:08
This reminds me of a similar title restoration issue when a second batch is executed from a first one this way:
Haha, the CMD is sooo buggy :roll:

Steffen

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: How to get rid of the extra character ahead of the title

#11 Post by jfl » 08 Feb 2021 01:42

aGerman wrote:
07 Feb 2021 14:20
Yes, VT sequences came again with the new console on Win 10. If you need something more downward compatible, try wrapping SetWindowText (SetConsoleTitle didn't work in my tests).
No, it's not needed in Windows 7, because the title command in Windows 7 does not add that extra space that is annoying me in Windows 10.
aGerman wrote:
07 Feb 2021 14:20
Haha, the CMD is sooo buggy :roll:
Indeed :-)

Which allows to achieve the same result without the start /b command:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set TITLE=%*
if not defined TITLE exit /b
for /f "tokens=1*" %%a in ('forfiles /p "%~dps0." /m "%~nxs0" /C "cmd /d /c echo(0x1B 0x07"') do set "ESC=%%a" & set "BEL=%%b"
<nul set /p "=%ESC%]0;%TITLE%%BEL%"
endlocal & %0
Recursively invoking itself without specifying a title preserves the title set by the first instance!

Jean-François

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get rid of the extra character ahead of the title

#12 Post by aGerman » 08 Feb 2021 14:30

Funny :lol: I tried to change the title in the recursion but this update was fleeting and only visible using pause. No clue what's going on. I mean, start /b creates another process which was the reason why I somehow expected to make the original process confused enough to keep the title. But neither your previous example nor this recursion is running in another process. So I'm not even sure if this is the same effect even if the symptoms are alike :?

Steffen

EDIT: For those who want to be able get rid of the "Administrator: " prefix or to set an empty title, here's a backward-compatible way:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
:: setlocal EnableDelayedExpansion

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Set a new window title, overwrite the "Administrator: " prefix in the console title of an elevated cmd process.
:: Syntax:
::   %force_new_title% [title]
::     title  New title of the console window.
::
setlocal DisableDelayedExpansion
for /f %%- in ('"prompt $H&for %%. in ("") do rem"') do set "bs=%%-"
set force_new_title=for %%- in (1 2) do if %%-==2 ((if not defined p set p= )^
%=% ^&setlocal EnableDelayedExpansion^&set "p=^^!p:'=''^^!"^&for /f "tokens=1* delims=%bs%" %%. in ("_%bs%^^!p:~1^^!") do^
%=% endlocal^&powershell.exe -nop -ep Bypass -c ^"^
%===% $c=Add-Type -Name pInv -PassThru -MemberDefinition '^
%=====% [DllImport(\"user32.dll\",CharSet=CharSet.Unicode)] public static extern void SetWindowTextW(IntPtr h,string t);^
%=====% [DllImport(\"kernel32.dll\")] public static extern IntPtr GetConsoleWindow();';^
%===% $c::SetWindowTextW($c::GetConsoleWindow(),'%%/');^"^
%=% ^&endlocal) else setlocal DisableDelayedExpansion^&set p=
endlocal&set "force_new_title=%force_new_title%"
if !!# neq # set "force_new_title=%force_new_title:^^!=!%"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: empty title
%force_new_title%
pause

:: exclamation points don't need to be escaped (regardless of whether delayed expansion is enabled),
:: literal quotes have to be escaped using a preceding backslash
%force_new_title% !foo! "a&b" \"c&d\" 'bar' `n %%{$_}^|?{} @() *works*
pause

Post Reply