Computing Staff
  • 2

Batch File To Replace Multiple Rows Of Text

  • 2

Hello,

Can anyone help please?

I would like to create a batch file which replaces not just one word or line in a file, but multiple lines, replacing the removed lines with some specified text. For example, let’s just say the lines that appear immediately below has the following entry:

“Hello this is a very complex scenario but I (Line 1)
believe with MS DOS, there is always a way (Line 2)
round the problem. (Line 3)
(Line 4)
Any help on this would be greatly appreciated. (Line 5)
Happy New Year to all forum members.” (Line 6)

I now need to replace Line 2, 3 and 4 with my some other text. It is worth pointing out my real-life scenario has a far greater number of lines to be replaced. Can this be down through a batch file?

Share

1 Answer

  1. This is hokey but it works.

    ============================================
    @echo off > newfile & setLocal enableDELAYedeXpansion

    set N=
    for /f “tokens=* delims= ” %%a in (my.txt) do (
    set /a N+=1
    if !N! equ 2 type replace.txt >> newfile
    if !N! neq 2 if !N! neq 3 if !N! neq 4 >> newfile echo.%%a
    )

    =====================================
    Life is too important to be taken seriously.

    M2

    • 0