## Need help understanding this parsing method???

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
IcarusLives
Posts: 122
Joined: 17 Jan 2016 23:55

### Need help understanding this parsing method???

Hello everyone

Has anyone heard from einstein1969? I have a script he wrote, and as I'm looking through the code I find something I don't quite understand, and was hoping someone could explain it to me.

Here is einstein1969's Heartbeat.bat, works perfectly fine here on win10.

Code: Select all

``````@echo off

setlocal EnableDelayedExpansion

if not "%1"=="" goto :%1

color 0f
mode 72,71
chcp 850
ping -n 2 127.0.0.1 >nul

"%~f0" HB 24 10 | find "TTL" | "%~f0" MAIN 4

exit /b

:MAIN

for /f "delims==" %%v in ('set') do set "%%v="

set /A "xres=70, yres=70

for /l %%y in (1 1 !yres!) do (
set "Lines%%y="
for /l %%x in (1 1 !xres!) do set "lines%%y=!lines%%y! "
)

set g=.Û

:: PI=31416

set "SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
set "SIN(x)=(a=(x)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %SIN%)"
set "SIN="

set t0=%time%

(

Rem Empty Env, use percent expand.
set SIN(x^)=
set xres=
set yres=

for /L %%N in (0) do (

set/p =
set/p =

set /a "t=1!time:~6,2!!time:~9,2!-10000, dt=t-(oldt+0), dt+=(dt>>31)&6000" >NUL
if !dt! geq %2 (

cls & for /l %%? in (1, 1,%yres%) do echo( !Lines%%?!

for /F "tokens=1-8 delims=:.," %%a in ("%t0: =0%:!time: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, angle=a/2"

rem set /A Angle+=3

For /L %%y in (0,1,%yres%) do (

set Lines%%y=!Lines%%y:°= !
set Lines%%y=!Lines%%y:±=°!
set Lines%%y=!Lines%%y:²=±!
set Lines%%y=!Lines%%y:Û=²!

)

if !dy! leq !dx! (
if !x2! lss !x1! set /A t=x2, x2=x1, x1=t, t=y2, y2=y1, y1=t

if !y2! gtr !y1! (
set /A yincr=1
) else set /A yincr=-1

set /a "d=2*dy-dx, Eincr=2*dy, NEincr=2*(dy-dx)"

For /F usebackq^ tokens^=1-3 %%x in ('!x1! !y1! 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"

set /A x1+=1
for /L %%X in (!x1!,1,!x2!) do (
if !d! lss 0 (
set /A d+=Eincr
) else set /A d+=NEincr, y1+=yincr

For /F usebackq^ tokens^=1-3 %%x in ('%%X !y1! 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"

)
) else (
if !y2! lss !y1! set /A t=x2, x2=x1, x1=t, t=y2, y2=y1, y1=t
if !x2! gtr !x1! (
set /A yincr=1
) else set /A yincr=-1
set /a "d=2*dx-dy, Eincr=2*dx, NEincr=2*(dx-dy)"

For /F usebackq^ tokens^=1-3 %%x in ('!x1! !y1! 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"

set /a y1+=1
for /L %%Y in (!y1!,1,!y2!) do (
if !d! lss 0 (
set /a d+=Eincr
) else set /A d+=NEincr, x1+=yincr
For /F usebackq^ tokens^=1-3 %%x in ('!x1! %%Y 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"
)
)

set /a oldt=t >NUL
)
)

)
exit/b

:HB

for /L %%N in (0,1,%2) do (

pathping -n -q 1 -p %3 127.0.0.1 >nul
start "HBPING" /B ping -n %2 127.0.0.1

)

for /L %%N in (0) do (

ping -n 2 127.0.0.1 >nul
start "HBPING" /B ping -n %2 127.0.0.1

)

exit /b``````
But the bit that I don't understand it this piece here

Code: Select all

``````For /F usebackq^ tokens^=1-3 %%x in ('!x1! !y1! 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"

But more particularly this portion

For /F usebackq^ tokens^=1-3
``````
Why the carets there??

Expert
Posts: 993
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

### Re: Need help understanding this parsing method???

The carets are because of the lack of quotes in the for loop options. Ordinarily it would be

Code: Select all

``For /F "usebackq tokens=1-3" %%x``
but it isn't here because... I'm really not sure. I can't find the specific version of the code that you're using. Presumably it's for performance optimization, but I can't be sure.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

### Re: Need help understanding this parsing method???

IcarusLives wrote:
25 Nov 2018 21:30
But the bit that I don't understand it this piece here

Code: Select all

``````For /F usebackq^ tokens^=1-3 %%x in ('!x1! !y1! 1') do set c=!Lines%%y:~%%x!& set "Lines%%y=!Lines%%y:~0,%%x!!g:~%%z,1!!c:~1!"

But more particularly this portion

For /F usebackq^ tokens^=1-3
``````
Why the carets there??
Syntax is For /F "options" %variable IN... so options has to be quoted or special characters escaped. So the code above is correct.