0

I was looking at the batch file which jeb created. [[HERE]][1] Someone can explain me some things?

Like, what does the function

for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do

do and what set "param=^%~2" ! and set "param=!param:"=\"!" and findstr /p /A:%1 "." "!param!\..\X" nul mean?

Full code:

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

rem Prepare a file "X" with only one dot
<nul > X set /p ".=."

call :color 07 "Premi un"
call :color 0a " tasto " 
call :color 07 "per continuare..."
pause

:color
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b

thanks to everyone in advance.

Edoardo Fiocchi
  • 141
  • 1
  • 7

1 Answers1

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

  1. 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
  2. prompt #$H#$E# sets escape and backspace characters (separated by #) to to the prompt (check prompt /?) and echo on makes it visible.
  3. for %%b in (1) do rem do nothings more but prints the prompt so it will be possible to be processed.
  4. Everything is put in double quotes so escaping & is not needed.
  5. As result the outer FOR command processes the string #backspace#escape#
  6. 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)

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • "rogue exclamation" :-/ ??? Never heard of it, and Google didn't help. Trying to relate the link content to the term did not help either. I consider the link to show how to protect a problem character by encoding it as two non-problem characters. – dbenham Jan 05 '17 at 18:04
  • @dbenham - in matter of fact [you called it like that](http://ss64.org/viewtopic.php?pid=6127#p6127) and this where I took the link from. It was pretty impressive technique :) – npocmaka Jan 05 '17 at 18:56
  • Ahh, I remember that one. I certainly wouldn't call the technique "simple". And I used that term only in response to your question about the "lonely `!`". That little hack is one component of a much larger hack. – dbenham Jan 05 '17 at 19:51
  • Thanks a lot npocmaka! It helped a lot! – Edoardo Fiocchi Jan 20 '17 at 15:10