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
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.
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.