Find and replace algorithm for string in text file using batch script, works, but stopping at random Line

  batch-file, script, text-files, windows

To preface this: This is my very first Batch script

I’ve been trying to figure out how to replace an entire line in a text file that contains a certain string using a Batch Script. I’ve found this solution provided by another user on Stack Overflow, which does the job, however, it just stops iterating through the text file at some random point and in turn, the output file is left with a bunch of lines untransferred from the original file. I’ve looked character by character, and line by line of the script to figure out what each part exactly does, and can not seem to spot what is causing this bug.

The code provided is:

From: Batch file: Replace content of a line in a text file by a word

Credit to this person:

copy nul output.txt
for /f "tokens=1* delims=:" %%a in ('findstr /n "^" file.txt') do call :do_line "%%b"
goto :eof

set line=%1
if {%line:String =%}=={%line%} (
  echo.%~1 >> output.txt
  goto :eof
echo string >> output.txt

The line it is stopping at does not have any unique characteristics that are not seen in any previous lines, and if that line is removed, the script will continue past that point, but then stop again at another random line.

Your help would be much appreciated.

Source: Windows Questions