0

I am able to do it with this code. It looks for a line that has the given string and replaces the entire line with the replacement string (the MatchLen is used to match a portion of the string). Also, it is limited in that it only finds lines that begin with the search string which works for my purposes. My big problem is that it doesn't copy blank lines (I tried the echo., but that only works when there is a space on the line). What do I need to do that?

@echo off &setlocal
set "InputFile=C:\MyInFile.txt"
set "OutputFile=C:\MyOutFile.txt"
set "ReplaceStr=REPLACEMENTREPLACEMENTREPLACEMENTREPLACEMENT"
set "MatchStr=SearchText goes here"
set MatchLen=20

(for /f "delims=" %%i in (%InputFile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "CurLine=!line:~0,%MatchLen%!"
    if !CurLine! equ !MatchStr! (
        echo %ReplaceStr%
    ) else (
        if not "%%i"=="" (
            echo(!line!
        ) else (
            echo.
        )
    )
    endlocal
))>"%OutputFile%"
del %InputFile%
rename %OutputFile%  %InputFile%
cashonly
  • 119
  • 9

0 Answers0