search and replace in a file using windows batch programming

batch-file

Solution

Replace the filename.txt and search1/replace1 with your filename and the proper search/replace strings or text.

 Echo off
    set "textfile=filename.txt"
    set "tempfile=filenametemp.txt"
    (for /f "delims=" %%i in (%textfile%) do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        set "line=!line:search1=replace1!"
        echo(!line!
        endlocal
    ))>"%tempfile%"
    del %textfile%
    rename %tempfile%  %textfile%

Problem

I have a batch file that I want to open a file and do a very simple search and replace of that file and then save (overwrite) the file it did the search and replace on. I know how to read the first line of a file: ``` set /p file= < file.txt ``` but struggling on the batch method of reading a whole file and doing search/replace on it.

Original source