for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
One by one.
- For command (with
/f
switch) parses the output of another command. In this case it takes the first and the second substring of the split by #
result
prompt #$H#$E#
sets escape and backspace characters (separated by #
) to to the prompt (check prompt /?
) and echo on
makes it visible.
for %%b in (1) do rem
do nothings more but prints the prompt so it will be possible to be processed.
- Everything is put in double quotes so escaping
&
is not needed.
- As result the outer FOR command processes the string
#backspace#escape#
- At the end backspace is assigned to
del
variable.
The rest is a subroutine used for coloring.
It uses a simple technique called rogue exclamation
used to safely assign strings that contains special characters.
On the second line it replaces "
with \"
- an escape of quotes needed by findstr.
The line <nul > X set /p ".=."
creates a file X
with single dot and without end of line character (see here).The file will be searched later by FINDSTR command.
Then is the coloring - try for example findstr /p /A:05 "." "something\..\X" nul
. This will search for a dot in a file with a relative path (the already created X in this case) and will color the path to the file.
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
. Finally this is used. It will print the backspace characters (again without using new line) so it will delete the not needed data in the findstr line that colorizes the text.The good thing in findstr
is that it colors the text independently of the 'global' console colors (unlike color
command)
In conclusion this uses non-documented hacks almost on each line , that's why is so hard for reading ( Jeb is a semi-legend in the batch scripting - most of tricks here are his invention)