I'm trying to output part of a file (the text enclosed between :begin(...) and :end(...) markers) using a BATCH script. The target is pure ASCII text but it may contain blank lines and symbols; the few answers I could read in SO don't handle arbitrary text correctly...
The code below is supposed to scan itself for the block in question and output its content:
@echo off
call :SelfExtractBlock "README.TXT"
exit /b %ErrorLevel%
:SelfExtractBlock
setlocal DisableDelayedExpansion
set enter=:begin(%~1)
set leave=:end(%~1)
set found=
for /f "delims=" %%a in ('findstr /n "^" "%~f0"') do (
set line=%%a
setlocal EnableDelayedExpansion
set text=!line:*:=!
if defined found (
if "!text!" == "!leave!" exit /b 0
echo(!text!
) else if "!text!" == "!enter!" (
set found=1
)
endlocal & set found=%found%
)
exit /b 1
rem ################# EMBEDDED FILES #################
:begin(README.TXT)
symbols: % ^ & < > | ' ` , ; = ( ) ! " \ [ ] " . * ?
escapes: %% ^^ ^& ^< ^> ^| ^' ^` ^, ^; ^= ^( ^) ^^! "" \\ \[ \] \" \. \* \?
variables: %var% !var!
:end(README.TXT)
But I don't get any output, while I'm expecting:
symbols: % ^ & < > | ' ` , ; = ( ) ! " \ [ ] " . * ?
escapes: %% ^^ ^& ^< ^> ^| ^' ^` ^, ^; ^= ^( ^) ^^! "" \\ \[ \] \" \. \* \?
variables: %var% !var!
What's going on? How do I fix the code?
endlocal & set found=%found%byfor %%f in ("!found!") do endlocal & set found=%%~f2. It is not necessary that"!leave!"and"!enter!"use Delayed Expansion because their values don't change inside theforforto define a variable that won't disappear because of anendlocal; nice trick!enterandleavecould contain something like:begin(<h1 class="">The rise of</h1>), won't that break theifcondition if you use%enter%instead of!enter!?