Code snippet inadvertently prevents console title renaming

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Code snippet inadvertently prevents console title renaming

#1 Post by Quisquose » 03 Nov 2018 02:51

I've been experimenting setting up different console sessions (configured for different purposes) using basic commands like title, color, prompt etc. via the cmd.exe /K switch.

After much trial and error (mainly error) I mostly got things working as I wanted. However there were certain instances where the session that I was trying to cutomize just would not implement the 'title' command. I was convinced that it was a syntax error (because I was using several commands at once, as well as specifying a script path with spaces in it, on the same line. So I assumed that I'd got my quote marks or command orders messed up some how (which is somewhat familiar occurrence for me).

However, after couple of days of hair-pulling, reading countless web pages, and trying all sorts of different approaches, I was getting nowhere. Eventually, I figured out that the issue was confined to a handful of scripts that happened to contain a useful code snippet that I acquired off the internet a couple of years ago. If I deleted that particular section of code from the script, then I could us the /K switch to change the console's window title. But as soon as I put it back, my title commands would be ignored again, regardless of where I tried to apply the change from (i.e. script, shortcut, Registry).

The code in question is beyond my limited understanding, so I wondered if any of the more accomplished among you might be prepared to take a look at it for me, in order to determine why it would prevent the console window's title from being renamed, and what the potential solution might be to stop this interference (assuming that there is one).

Thanks!

Code: Select all

::define a variable containing a single backspace character (to allow spaces to display at beginning of line)
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
:: use the %BS% variable (created above) to allow spaces to be added to start of text string below.
set /p c=%BS%  Delete Icon Cache File?  [Y/N] 
if /I "%c%" EQU "Y" goto :Proceed
if /I "%c%" EQU "N" goto :NotProceed
goto :choice

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

Re: Code snippet inadvertently prevents console title renaming

#2 Post by Squashman » 03 Nov 2018 06:14

Just tried your code and made the first line of the code be a TITLE statement. Then put more TITLE statements in the code.
The Window Title changed every time.

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Code snippet inadvertently prevents console title renaming

#3 Post by Quisquose » 03 Nov 2018 15:12

Squashman wrote:
03 Nov 2018 06:14
Just tried your code and made the first line of the code be a TITLE statement. Then put more TITLE statements in the code.
The Window Title changed every time.
Thank you for letting me know the result of your testing, Squashman.

It seems as if these things are never as straight-forward as I imagine (or hope) them to be.

In an attempt to keep my post concise and to the point, I tried to focus only on just what I thought was relevant, but usually it turns out that doing this is a mistake because there's often some (seemingly unrelated) other factor at play that I didn't mention at the time for the sake of brevity.

With that in mind, here is a fuller explanation of what I am trying to achieve, and maybe this will shed light on the issue. I should point out that by me replying to your post, it does not mean that I am expecting you personally to address the questions I have below. I'm just putting this out there generally, in the hope that anyone can help.


Background

I'm using Windows 7 x64 and I have just a few basic autorun console commands listed in:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Code: Select all

title Terminal & color 2E & cls
Those are the general settings that I'd like to have applied system-wide to console windows, but only in the absence of any other over-riding settings (such as when running cmd.exe directly from: Start > Run > cmd).

Then I have various different shortcuts to cmd.exe where I use the 'Target' field in the Properties of those shortcuts to over-ride and change the default settings that are stored in AutoRun in the Registry. These settings differ from shortcut-to-shortcut.

Finally, I have various scripts that may be run from any of those cmd.exe shortcuts. Again, these may contain customizations (e.g. title and color etc.) that are specific to that script, instead of the shortcut console in which the script is being run.

So far, so good. If I run a cmd.exe without any title command switch specified, it will display the title stored in the Registry AutoRun Key. If I run cmd via a Start Menu shortcut that has a title specified in its 'Target' field, then the shortcut will correctly display its own title (instead of the default Registry one). If I use that exact same shortcut again, but this time adding a path to a specific script that contains its own title and color settings, then the title and color specified within the script will correctly over-ride the settings specified in the shortcut from which it was run. This is all great.

However, if I use this Test shortcut

Code: Select all

C:\Windows\System32\cmd.exe /D /K color d1 & title Rebuild Icon Cache & "U:\Users\XXX\Files\Documents\Code\Batch Files\rebuild_icon_cache.cmd" & prompt Testing-prompt $g
to run the script below (which has NO title specified in it), then instead of getting the title Rebuild Icon Cache (as specified by the shortcut) I instead get the title Terminal (which is the default title that I assigned in the Registry AutoRun key).

If I leave everything exactly the same, but just remove the code section that I quoted in my first post of this thread, then the console window title correctly displays the name specified by the shortcut. And if a title is added into the script itself, that that too will display correctly (just as long as the code snippet in question is not present).

So that is why I identified that particular section of code as the culprit.

Here is the full script that I am running (quoted in its entirety so that nothing is overlooked).

Code: Select all

:: Created by: Shawn Brink
:: http://www.sevenforums.com
:: Tutorial:  http://www.sevenforums.com/tutorials/49819-icon-cache-rebuild.html
:: Modified by Me



@echo off 
echo.
echo.  DELETE ICON CACHE
echo.  -----------------


:choice
echo.
echo.
echo.  To delete the Icon Cache, Windows Explorer must be forcibly restarted. 
echo.
echo.  Therefore  SAVE ALL OPEN WORK  before continuing.
echo.
echo.
echo.
set iconcache=%localappdata%\IconCache.db

::define a variable containing a single backspace character (to allow spaces to display at beginning of line)
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
:: use the %BS% variable (created above) to allow spaces to be added to start of text string below.
set /p c=%BS%  Delete Icon Cache File?  [Y/N] 
if /I "%c%" EQU "Y" goto :Proceed
if /I "%c%" EQU "N" goto :NotProceed
goto :choice


:Proceed
If exist "%iconcache%" goto delID
REM @CLS
echo.
echo.
echo.
echo.
echo.
echo.
REM  Prefix desired display text with   echo|set /p =   (and wrap text in quotes) to get the cursor to stay on same line.
REM  Insert the %BS% variable (created earlier in this script) to be able to use spaces at the start of display text.
echo|set /p ="%BS%  Icon DB could NOT be found.  It has ALREADY been deleted. "  
REM  I'm using this ping command in place of the 'Timeout' command (because Timeout can only work in whole seconds).
ping.exe -n 2 -w 1 127.0.0.1 >nul 2>&1
echo.
echo.
echo.
echo|set /p ="%BS%  Press any key to exit ... " 
pause /f >nul 2>&1
exit /B


:delID
REM  Kill the 'PS Tray Factory' tray icon hiding program before killing Windows Explorer (otherwise PS Tray Factory crashes).
taskkill /IM psTrayFactory.exe /F >nul 2>&1
@CLS
echo.
echo.  Attempting to delete Icon DB ...
echo.
TIMEOUT /T 2 /NOBREAK >nul 2>&1
@CLS
ie4uinit.exe -ClearIconCache
taskkill /IM explorer.exe /F >nul 2>&1
del "%iconcache%" /A
start /b explorer.exe
echo.
echo.
echo.  Icon DB file has SUCCESSFULLY been deleted. 
echo.
echo.  Your icon cache will be re-built once you re-start your computer.
TIMEOUT /T 2 /NOBREAK >nul 2>&1
"C:\Program Files (x86)\PS Tray Factory\PSTrayFactory.exe" /start
echo.
echo.
echo.
echo.
echo.
echo.
echo.  Press any key to exit ...
pause /f >nul 2>&1
goto :Exit


:NotProceed
echo.
echo.
REM  This modified echo command makes the cursor stay on the same line as the output display text.
REM  Using   set /p  on it own (without piping) works, but it halts the script (because /p waits for user input).
echo|set /p ="%BS%  Closing console ... "
ping.exe -n 2 -w 1 127.0.0.1 >nul 2>&1
goto :Exit


:Exit
exit



My Questions

  • 1. Why does that section of code (as quoted in my first post in this thread) cause the title specified in the shortcut to be ignored (and the default AutoRun title to get displayed instead)?
  • 2. Although my test shortcut (as quoted above) works fine with regards to console window titling (as long as the culprit code is absent), it does still display the path and name of any script that is being run from that shortcut.

    Image
      
    How can I hide the script path from the console window title bar (using only the 'Target' field of the Properties of the shortcut itself) so that only the window name is displayed? (BTW, I spent hours trying out countless different ways to do this, but I got nowhere).
  • 3. For situations where a script does not need to be run as administrator, how can I correctly integrate the runas command (example shown below) so as to remove the word "Administrator" from the title bar of the console window?

    Code: Select all

    runas /trustlevel:0x20000 "cmd /k title My Awesome Command Prompt"
    This works when pasted directly into a console window, but I'd like to know how to integrate it into a shortcut Target field (for example, using the shortcut that I quoted earlier in this post) because when Target paths become more complicated (i.e. containing multiple switches and a full script path) then it is not obvious to me where exactly the new command should be inserted, and whether any alteration is required (e.g. additional quote marks etc,).
  • 4. Also, how can I assign that same runas /trustlevel command to a macro, so that it can be placed in batch file and invoked by using a short typed 'alias' command. I have tried testing it out, but when it's run from a script it opens a new window. However, when typed into a console directly, the runas command is applied to the existing window into which the command was typed (which is the behavior that I'd like to retain even when it's run from a script or from the Target box of a shortcut file).
  • 5. I have seen some examples quoted online where a second cmd entry is used as part of a command. For example here:

    Code: Select all

    C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f
    I've also seen similar commands where the second mention of cmd does not have a preceding minus sign in front of it. What is the purpose of this additional 'cmd' entry, and what difference does the minus sign make? Having tested some examples myself, it's not apparent to me what it's doing.
  • 6. Lastly, my script example (shown earlier) includes a command to kill explorer and restart it. After this happens, the command console window loses focus, which means that further key presses are not directed into the console. Is there any way that window focus can be retained by (or returned to) the console? I'm not sure whether this loss of focus is due to the manner in which I launched Explorer or PS Tray Factory (the other program mentioned in my script) that is possibly causing them to grab focus, or whether losing window focus is just part of any Explorer restart.Regardless of the cause, I'd really like to be able to somehow finish the script with the console window still focussed.
Thanks in advance for any assistance that anyone can offer.

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Code snippet inadvertently prevents console title renaming

#4 Post by pieh-ejdsch » 04 Nov 2018 10:45

Hello Quisquose,

2.
Try writing the shortcut like this:

Code: Select all

cmd /k "prompt $g $S & pushD "C:\Users\username\Desktop\CMD test" & title "Rebuild Icon Cache" & color d1 & Program & prompt Testing-prompt $g"
You either have to enter the path into the variable Path or use PushD to switch to the directory where your batch is located.
Thus, only the name of the currently called command or program is displayed during execution.

5.
There are different versions with a command line once in the dialog Execute and then in the command line / shortcut
the dialog Execute behaves a little differently.
Once leaving a CMD with exit results in the next command being executed.
1. Only in the command line:

Code: Select all

cmd /k prompt $g & cmd /k echo done
2. Now it behaves nested: (the dialog Execute behaves like point 1)

Code: Select all

cmd /k "prompt $g & cmd /k echo done"
A nested / following CMD instance ensures that in case of a fatal error of a program
the external CMD instance is maintained or the following is executed.

Phil

Post Reply