The command FOR always ignores empty lines and by default it ignores also lines starting with a semicolon. On usage of delayed expansion also lines containing an exclamation mark are modified on execution of command line set nl=%%a in the loop. So a solution using FOR could be the completely wrong attempt depending on content of file t1.txt.
Download JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript and store it in same directory as the batch file below.
@echo off
if not exist "%~dp0jrepl.bat" goto :EOF
if not exist "t1.txt" goto :EOF
call "%~dp0jrepl.bat" "^.*textto.*$" "replace with this text" /F "t1.txt" /O "t2.txt"
rem Compare the two files binary with ignoring the differences. Delete file
rem t1.txt if a line was modified by JREPL.BAT. Otherwise move file t1.txt
rem over t2.txt to keep the last modification date of file t1.txt for t2.txt.
%SystemRoot%\System32\fc.exe /B "t1.txt" "t2.txt" >nul
if errorlevel 1 ( del "t1.txt" ) else move /Y "t1.txt" "t2.txt"
jrepl.bat searches with a regular expression for lines containing textto and replaces all such lines by replace with this text. All other lines are copied unmodified from input file t1.txt to output file t2.txt.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
fc /?
goto /?
if /?
move /?
rem /?
jrepl.bat /?