How to send variable to a background process, which is a loop :label of the bat itself (or stop loop if variable defined)
My approach to this is to use an infinite for /l loop in a label that is started as a seperate thread. I use an ADS of the file to communicate to the thread from the main file when to stop, then a hard Exit to quit the thread.
:# Author: T3RRY Created: 03/06/2021
:# Purpose: RGB Spinner animation for load / waiting indication while script continues to run.
:# Utilises a second thread to display animation while the main script continues executing.
:# Execution and termination of the thread is controlled using a value stored in an alternate data
:# stream of this file:. 1 = run ; 0 = stop
@Echo off & CD /D "%~dp0"
:# thread label structure by Jeb - https://stackoverflow.com/a/68636825/12343998
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
Call :defMacros
%= Quit if not NTFS =% If Errorlevel 1 Exit /b
Cls
===:# Usage Examples.
%Start.Thread:MSG=Example 1%
For /l %%i in (1 1 100000)Do Set "var=%%i"
%End.Thread%
Echo([%var%] iterations actioned.
%Start.Thread:MSG=Example 2%
For /l %%i in (1 1 10000)Do Set "var=%%i"
%End.Thread%
Echo([%var%] iterations actioned.
%Start.Thread:MSG=Example 3%
Set "var=1"
%End.Thread%
Echo([%var%] iterations actioned.
:# End script cleanup
Powershell -c "remove-item -path '%~nx0' -Stream '*'"
CHCP %active.cp% > nul
@Pause
Goto :Eof
===========================================================
:AnimThread - Animation; RGB or BW spinner
:# Animation sequence. Symbols will not display correctly with certain fonts
:# Recommended font: Lucida Console
:# Alt 205 ═ 186 ║ 187 ╗ 200 ╚ 188 ╝ 201 ╔
:# ╔═╗╔═╗╔═╗╔═╗
:# ║/║║-║║\║║|║
:# ╚═╝╚═╝╚═╝╚═╝
:#
:# Framerate: ~ 25 fps. ('Delay' assigned as 4/100ths of a second.)
:# NOTE - Multiple instances of this thread can not be run in parallel;
:# as Virtual terminal Cursor save operation will overide information stored by parallel threads
Setlocal EnableDelayedExpansion
CHCP 65001 > nul
If "%_Param%"=="MSG" Set "_Param=Executing"
If defined Win10 <nul set /p "=%\E%[1E%_Param%.%\E%[?25l%\E%[1E%\E%7"
Set "title.i=-1"
Set "T2[L]=0" %= Rem Spinner Loop count =%
Set "T2[chars]= |/-\" %= Rem Spinner characters =%
Set "Delay=4" %= Framerate in Centiseconds =%
:# frame control via elapsed time by Dave Benham - https://www.dostips.com/forum/viewtopic.php?f=3&t=4741
If defined Win10 For /l %%. in ()Do (
%= Check completion status flagged in status stream of this file; =%
(For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do if %%G EQU 0 (
<nul Set /P "=%\E%[?25h%\E%8%\E[K%" %= restore cursor =%
%restore.CP% > nul %= restore codepage =%
<nul Set /P "=%\E%[7;32mCompleted.%\E%[0m%\E%[1E%\E%[M%\E%[K" %= notify completion; Emit newline; Delete last line of animation =%
(TITLE )
Exit %= exit thread =%
))2> nul
%= Calculate time elapsed =%
for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100
if !tDiff! geq !delay! (
Set /A title.i+=1
If !title.i! GTR 9 Set "title.i=0"
Set "Bar=║0123456789║"
For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
Title Processing... !BAR! !Time!
%= delay has expired ; display frame =%
Set /A "T2[L]+=1" %= Increment index point of spinner string =%
%= Output Spinner frame. Overwites previous output using saved cursor position =%
For /f "Delims=" %%v in ("!T2[L]!")Do <nul Set /p "=%\E%8%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╔═╗╔═╗╔═╗%\E%[0;38;2;150;;150m%\E%[K%\E%[E║%\E%[38;2;20;20;20;7m%\E%[48;2;255;;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;255;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;100;255m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║%\E%[0m%\E%[K%\E%[E%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╚═╝╚═╝╚═╝%\E%[0m%\E%[K%\E%[1G%\E%[2A%\E%7"
If !T2[L]! EQU 4 Set "T2[L]=0" %= Reset index point of spinner string =%
set /a t1=t2
)
)
%= If not defined Win10 [ No Virtual Terminal support ] =%
For /l %%. in ()Do (
(For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do if %%G EQU 0 (
%restore.CP% > nul
Cls
Echo(Complete.
(TITLE )
Exit
))2> nul
for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
if !tDiff! lss 0 set /a tDiff+=24*60*60*100
if !tDiff! geq !delay! (
Set /A title.i+=1
If !title.i! GTR 9 Set "title.i=0"
Set "Bar=║0123456789║"
For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
Title Processing... !BAR! !Time!
Set /A "T2[L]+=1"
For /f "Delims=" %%v in ("!T2[L]!")Do (
Cls
Echo(%_Param%.
Echo(╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗
Echo(║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║
Echo(╚═╝╚═╝╚═╝╚═╝╚═╝╚═╝
)
If !T2[L]! EQU 4 Set "T2[L]=0"
set /a t1=t2
)
)
:defMacros
:# OS Requirement tests
:# Verify NTFS drive
(Echo(verify.NTFS >"%~f0:Status") || (
Echo(This file must be located on an NTFS drive as it utilises alternate data streams.
Timeout /t 5
Exit /B 1
)
:# Windows Version control. Assigns flag true if system is windows 10 build GTR 10586
:# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
:# Version 1511 build number = 10.0.10586
Set "Win10="
For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "Win10=True"
:# If Win10 true ; Test if virtual terminal codes enabled ; enable if false
:# removes win10 flag definition if version does not support Virtual Terminal sequences
If defined Win10 (
Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel REG_DWORD 0x1" > nul || (
Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
) > Nul || Set "Win10="
)
=====:# Store codepage to restore on script completion
For /f "tokens=4 Delims=: " %%1 in ('CHCP')Do Set "active.cp=%%1"
=====:# Define Escape character for Virtual terminal sequences
If Defined Win10 For /f "Delims=" %%e in ('Echo(Prompt $E^|Cmd')Do Set "\E=%%e"
==================:# macros start and stop Thread via t/f flag written to alternate data stream.
:# Start.Thread - Save current Codepage;
:# Remove status thread from this file if present;
:# Notify command has started;
:# Flag status 1 in Stream :status;
:# Use start /b with params to relaunch this script and execute second thread
:# executes infinite loop in AnimThread1 based on win10 virtual terminal support
Set "Start.Thread=Set "_param=MSG"& (For /F "tokens=2 Delims=:" %%G in ('CHCP')Do Set "restore.CP=CHCP %%G")&((Echo(1)>"%~f0:Stream") & Start /AboveNormal /b "" "%~d0\:AnimThread:\..%~pnx0""
:# End.Thread - Flag status 0 in Stream :Status;
:# Execute a small delay to ensure thread has time to enable cursor display and clear animation.
:# [Delay of approx. 1/66th of a second]
Set "End.Thread=((Echo(0)>"%~f0:Stream")&For /L %%i in (1 1 750)Do ((Call ))"
Exit /b 0