Rob van der Woude's Scripting Pages

(intermediate) results for the

GetTitle batch challenge

Remember the challenge?

Write a batch file that will read and save its own window title.

After the first results were tested, additional conditions were formulated:

Seven "contestants" sent me eight scripts.
Carlos M.'s batch file does not really qualify for the challenge, as it generates VBScript and an executable to complete the task (which it does flawlessly).

The scripts presented here are the latest versions. Some were written before the first publication of the intermediate results, some after.
To me the challenge is not a contest, but more like a brainstorm session. I think everybody learned from it.

 

Tests

Five tests were performed on each batch file, each test on Windows 7 Ultimate SP1 (64-bit) as well as Windows XP Professional SP3 (32-bit):

  1. Run a single instance:
    START "Passed the first test" CMD /K GetTitle.bat
  2. Run a single instance, but with multiple console windows open:
    FOR /L %%A IN (1,1,5) DO START "Failed" CMD /K
    START "Passed" CMD /K GetTitle.bat
    FOR /L %%A IN (1,1,5) DO START "Failed" CMD /K

    Batch files that do not check their own PID will fail this test.
  3. Run multiple simultaneous instances, each with a different title:
    FOR /L %%A IN (10,1,20) DO START "Test %%A" CMD /K GetTitle.bat
    This is the toughest test, that only one batch file passed so far.
    Batch files that use predefined temporary file names or temporary titles, or that do not check their own PID, will all fail this test.
    Even %Random% isn't random enough for creating truly random file names or temporary titles.
  4. Windows 7 poses an extra challenge: to run a console window requires elevated privileges, and the elevated privileges are advertised in the title bar by an Administrator: prefix.
    If the prefix is not stripped, restoring the original title will result in a double Administrator: prefix.
    I am not sure if the prefix is language dependent, but just stripping the string Administrator: from the title may not be enough (besides, it might be part of the intended title).
  5. Is the command line, which is appended to the title when the batch file is started by doubleclicking or by the START command, trimmed from the result?
    This was not a requirement, and it is ignored in the other tests.
    I just mention it, because the command line needs to be trimmed anyway, if the title is read in order to restore it later.

 

Author
(click for source code)
Test 1:
does it work at all?
Test 2:
multiple console windows
Test 3:
multiple simultaneous instances
Test 4:
strip prefix only in Win7
Test 5:
strip appended command line
XP Win7 XP Win7 XP Win7 XP (1) Win7 XP Win7
Batch Files
Antonie v.d. Baan Passed Passed Passed Passed Failed Failed Passed Passed No No
Brian Williams Passed Passed Passed Passed Passed Passed Passed Passed Yes Yes
Jean François Larvoire Passed Failed Passed N/A Failed N/A Passed N/A No N/A
Jiri Hofreiter (with WMIC) Passed Passed Passed Passed Failed Failed Passed Passed No No
Jiri Hofreiter (without WMIC) Passed Passed Passed Passed Failed Failed Passed Passed No No
Justin Goldspring Passed Failed Passed N/A Failed N/A Passed N/A Yes N/A
Menno Vogels Passed Passed Passed Passed Failed Failed Passed Passed No No
 
Author
(click for source code)
Test 1:
does it work at all?
Test 2:
multiple console windows
Test 3:
multiple simultaneous instances
Test 4:
strip prefix only in Win7
Test 5:
strip appended command line
XP Win7 XP Win7 XP Win7 XP (1) Win7 XP Win7
Executables
Carlos M. Passed Passed Passed Passed Passed Passed Passed Passed Yes Yes
Rob van der Woude Passed Passed Passed Passed Passed Passed Passed Passed Yes Yes

 

Notes: 1 In XP, nothing should be stripped from the title.
Tested with the command START "Administrator: test" CMD /K GetTitle.bat
By the way: in Windows 7 this command does not result in a double Administrator: prefix, TITLE Administrator: test does.
  2 The failures of the batch files by Jean François Larvoire and Justin Goldspring in Windows 7 are caused by the Administrator: prefix.
We're still working on it.
  3 Jiri Hofreiter's GetTitle.bat without WMIC more or less did pass most tests, but in case the batch file is called by its name only (no path, nor extension) it may append the batch file's extension when restoring the title.
We're still working on it.

💾   Download all contestants' sources.

 

Conclusions

All "contestants" seem to agree on the use of TASKLIST to retrieve the window title.

Correctly running multiple instances of the batch files was the hardest requirement to meet.
The %Random% variable starts with the same value in every newly opened console window. This makes it almost impossible to create truly random temporary file names or temporary titles.
For that very reason I wrote GetTitle.exe in C# when I needed to reliably read the title.
However, Brian Williams' latest version proves that it is possible in batch.

A way to make random numbers more random is by using the fraction of seconds of the current time.
WMI's Win32_LocalTime class looks promising, but unfortunately the MilliSeconds property is not implemented in Windows.
The closest alternative is the fraction (hundredths) of seconds in the TIME command's output (not TIME /T as it won't even display the seconds):

FOR /F "tokens=1* delims=:" %%A IN ('VER ^| TIME') DO (
	FOR /F "tokens=3,4 delims=:.," %%C IN ("%%~B") DO (
		SET AlmostTrueRandom=%Random%%%C%%D
	)
)

The code above appends the seconds (%%C) and hundredths of seconds (%%D) to a not so random %Random% number.
%Random% can even be left out, as Brian Williams' solution proves.

However, on my fast Windows 7 test system, sometimes 4 console windows were started within a 10 microseconds interval, so the starting time was not a reliable source for a unique number.
Brian solved that problem not by making a better random generator, but by waiting approximately one second (PING), checking for duplicate temporary titles, and changing them to the then current time if a duplicate was found. The process is repeated until there are no more duplicates left.
Brilliant in its simplicity.


I would like to thank all "contestants" for sharing their ideas, research and time.
And whenever we find better solutions, the published source code will be updated.


page last modified: 2022-10-26; loaded in 0.0073 seconds