simple Batch based Tail
Posted: 14 Dec 2022 20:37
@echo off
chcp 65001
Mode 151,49
REM A simple Tail like pure batch script
REM Set initial default Lines to print
set linesbefore=1024
set follow=yes
if "%~1"=="" echo Usage: tail -n lines filename & goto :eof
if "%~1" equ "-n" (set follow=no& set "linesbefore=%2" & set filename=%~3) else (set filename=%~1)
if "%~1" equ "-nl" (REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000000 /f >nul & goto :eof)
setlocal enabledelayedexpansion enableextensions
REM
REM Iterate through and print newly added Lines.
set LineBufferNr=0
set LineBufferArray=x
set prevFileLineNr=0
REM Check the tail entries value or set it if appropriate
REG query HKCU\Console\ /v taillock 1>Nul 2>Nul
if "%errorlevel%" equ "0" for /F "delims=" %%j in ('REG query HKCU\Console\ /v taillock 2^>NUL') DO (set Value=%%j)
if "!value!" EQU " taillock REG_BINARY 00000001" echo Info HKCU\Console\taillock has been found. & Pause
where %filename% 1>NUL & IF !errorlevel! GTR 0 (echo theres no File %FileName%) & goto :eof
:think
REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000001 /f >nul
REM for /F "tokens=2 delims=:" %n in ('find /V /C "" "%filename%"') do Set /a FileLines=%n
REM store newly added Lines in the array and print them
set /a FileLineNr=0
FOR /f "tokens=*" %%b IN ('type %filename%') DO (
set /a FileLineNr+=1
if "%%b" neq "" set /a LineBufferNr+=1 & set LineBufferArray!LineBufferNr!=%%b
if !FileLineNr! equ !prevFileLineNr! (set /a skipLines=!LineBufferNr!+1)
)
if !FileLineNr! gtr !prevFileLineNr! (
set /a newLines =!FileLineNr!-!prevFileLineNr!
REM echo Debug: Recieved !newLines! new lines
REM initially print Files lines up to the length of the lineBuffer
if !LinesBefore! gtr %LineBufferNr% set /a LinesBefore=!LineBufferNr!
if !LinesBefore! gtr 0 set /a LinesBefore=%LineBufferNr%-!LinesBefore!+1 & set /a skipLines=!LinesBefore!
for /L %%o in (!skipLines!,1,!LineBufferNr!) do (echo !LineBufferArray%%o!)
set LinesBefore=0
set prevFileLineNr=%FileLineNr%
)
:wait
set prevFileLineNr=!FileLineNr!
set LineBufferNr=1
REM play or Debate internally for some time.
for /l %%i in (1,1,200) do (
echo debate internally and decide friendly democratic and open with everyone involved >NUL
)
if "%follow%" equ "yes" goto think else goto en
:en
REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000000 /f >nul
chcp 65001
Mode 151,49
REM A simple Tail like pure batch script
REM Set initial default Lines to print
set linesbefore=1024
set follow=yes
if "%~1"=="" echo Usage: tail -n lines filename & goto :eof
if "%~1" equ "-n" (set follow=no& set "linesbefore=%2" & set filename=%~3) else (set filename=%~1)
if "%~1" equ "-nl" (REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000000 /f >nul & goto :eof)
setlocal enabledelayedexpansion enableextensions
REM
REM Iterate through and print newly added Lines.
set LineBufferNr=0
set LineBufferArray=x
set prevFileLineNr=0
REM Check the tail entries value or set it if appropriate
REG query HKCU\Console\ /v taillock 1>Nul 2>Nul
if "%errorlevel%" equ "0" for /F "delims=" %%j in ('REG query HKCU\Console\ /v taillock 2^>NUL') DO (set Value=%%j)
if "!value!" EQU " taillock REG_BINARY 00000001" echo Info HKCU\Console\taillock has been found. & Pause
where %filename% 1>NUL & IF !errorlevel! GTR 0 (echo theres no File %FileName%) & goto :eof
:think
REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000001 /f >nul
REM for /F "tokens=2 delims=:" %n in ('find /V /C "" "%filename%"') do Set /a FileLines=%n
REM store newly added Lines in the array and print them
set /a FileLineNr=0
FOR /f "tokens=*" %%b IN ('type %filename%') DO (
set /a FileLineNr+=1
if "%%b" neq "" set /a LineBufferNr+=1 & set LineBufferArray!LineBufferNr!=%%b
if !FileLineNr! equ !prevFileLineNr! (set /a skipLines=!LineBufferNr!+1)
)
if !FileLineNr! gtr !prevFileLineNr! (
set /a newLines =!FileLineNr!-!prevFileLineNr!
REM echo Debug: Recieved !newLines! new lines
REM initially print Files lines up to the length of the lineBuffer
if !LinesBefore! gtr %LineBufferNr% set /a LinesBefore=!LineBufferNr!
if !LinesBefore! gtr 0 set /a LinesBefore=%LineBufferNr%-!LinesBefore!+1 & set /a skipLines=!LinesBefore!
for /L %%o in (!skipLines!,1,!LineBufferNr!) do (echo !LineBufferArray%%o!)
set LinesBefore=0
set prevFileLineNr=%FileLineNr%
)
:wait
set prevFileLineNr=!FileLineNr!
set LineBufferNr=1
REM play or Debate internally for some time.
for /l %%i in (1,1,200) do (
echo debate internally and decide friendly democratic and open with everyone involved >NUL
)
if "%follow%" equ "yes" goto think else goto en
:en
REG add HKCU\Console\ /v taillock /t REG_BINARY /d 00000000 /f >nul